Code Monkey home page Code Monkey logo

python-cmdline-bootstrap's Introduction

python-cmdline-bootstrap

This is a structure template for Python command line applications, ready to be released and distributed via setuptools/PyPI/pip for Python 2 and 3.

Please have a look at the corresponding article: http://gehrcke.de/2014/02/distributing-a-python-command-line-application/

Usage

Clone this repository and adopt the bootstrap structure for your own project. This is just a starting point, but I hope a good one. From there on, you should read and follow https://packaging.python.org/, the definite resource on Python packaging.

Behavior

Flexible invocation

The application can be run right from the source directory, in different ways:

  1. Treating the bootstrap directory as a package and as the main script:

    $ python -m bootstrap arg1 arg2
    Executing bootstrap version 0.2.0.
    List of argument strings: ['arg1', 'arg2']
    Stuff and Boo():
    <class 'bootstrap.stuff.Stuff'>
    <bootstrap.bootstrap.Boo object at 0x7f43d9f65a90>
  2. Using setup.py develop (documented here):

    # This installs the bootstrap command linking back
    # to the current checkout, quite neat for development!
    $ python setup.py develop
    ...
    $ bootstrap arg1 arg2
  3. Using the bootstrap-runner.py wrapper:

    $ ./bootstrap-runner.py arg1 arg2
    Executing bootstrap version 0.2.0.
    List of argument strings: ['arg1', 'arg2']
    Stuff and Boo():
    <class 'bootstrap.stuff.Stuff'>
    <bootstrap.bootstrap.Boo object at 0x7f149554ead0>

Installation sets up bootstrap command

Situation before installation:

$ bootstrap
bash: bootstrap: command not found

Installation right from the source tree (or via pip from PyPI):

$ python setup.py install

Now, the bootstrap command is available:

$ bootstrap arg1 arg2
Executing bootstrap version 0.2.0.
List of argument strings: ['arg1', 'arg2']
Stuff and Boo():
<class 'bootstrap.stuff.Stuff'>
<bootstrap.bootstrap.Boo object at 0x7f366749a190>

On Unix-like systems, the installation places a bootstrap script into a centralized bin directory, which should be in your PATH. On Windows, bootstrap.exe is placed into a centralized Scripts directory which should also be in your PATH.

python-cmdline-bootstrap's People

Contributors

jgehrcke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

python-cmdline-bootstrap's Issues

ValueError: Attempted relative import in non-package

When I try to run python bootstrap from the root of the project, I get this error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/jdevera/devel/github/python-cmdline-bootstrap/bootstrap/__main__.py", line 7, in <module>
    from .bootstrap import main
ValueError: Attempted relative import in non-package

Is this not one of the intended uses of this __main__.py file?

If I do python -m bootstrap then it works fine.

Somehow console script is not available

Sorry to write it here, but I don't know what to do.

My package yagmail is not getting a console_scripts entry point, even though with the exception of version, I completely followed your style, it seemed to all work out.

Could you see what is wrong with it? Code works under Python 2 and 3.

Also, could it be that python setup.py bdist_wininst gives an error after defining such an entry point on a Mac?

How to modify "setup.py"(entry_points) when I want to generate multiple command-line applications from sub-module and in a python package

I want to write a small personal package based on your scripts.
The structure of the directory is showed here:
I further add a new module (src) and script (case.py) under bootstrap fold like this:

python-cmdline-bootstrap/
...
├── bootstrap
│   ├── __init__.py
│   ├── __main__.py
│   ├── bootstrap.py
│   ├── stuff.py
│   ├── src
│       ├── __init__.py
│       ├── case.py
├── bootstrap-runner.py
...

the content of case.py showed below:

# -*- coding: utf-8 -*-

import argparse


def case():
    print("This a new command.")

I add the following lines into the setup.py:

console_scripts = """
[console_scripts]
bootstrap = bootstrap.bootstrap:main
cccase = bootstrap.src.case:case
"""

When I execute python setup.py install and run cccase in the terminal after installation, it shows the error:

Traceback (most recent call last):
  File "/home/chxp/tmp/python3-test/bin/cccase", line 11, in <module>
    load_entry_point('cmdline-bootstrap==0.2.0', 'console_scripts', 'cccase')()
  File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
    return ep.load()
  File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2443, in load
    return self.resolve()
  File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2449, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
ModuleNotFoundError: No module named 'bootstrap.src'

It works fine if I use ./bootstrap-runner.py or python -m bootstrap (I modify them also), I think it may a mistake in the setup.py.
Thus, how do I revise the setup.py? I want to generate different command-line applications in a python package.

Meanwhile, how do I test multiple command-line applications by using ./bootstrap-runner.py or python -m bootstrap in a single run.
It seems I need to change the content in __main__.py or bootstrap-runner.py to test each command-line application, such as bootstrap, cccase.

The original scripts are upload at " https://gitee.com/chxp/python-cmdline-bootstrap " and download by git clone https://gitee.com/chxp/python-cmdline-bootstrap.git.

Thanks for your help. (I also asked it on Stackoverflow: https://stackoverflow.com/questions/66954048/how-to-write-the-accurate-setup-pyentry-points-when-i-want-to-generate-multi )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.