Python installable package

Viewed 3320

So I am doing a project where I have to create an installable Python package, install it on my desktop (Windows OS) and run the application contained in the folder. After packing and installing the code, I get 2 folders in the path where pip is installed:

project_pkg
project_pkg-0.0.1.dist-info

In the project_pkg folder I have 1 folder and 2 files:

__pycache__
__init__.py
project.py

What I want to do is run project.py via the cmd. How can I do it? The file init.py contains only this:

name = 'project'

UPDATE

After reading @Patrick Allen answer I figured out everything.Now I can run the application just by typing project [args]. When I do it the up runs as expected and the output is correct, but when it's about to finish, I get this weird error:

Traceback (most recent call last):
File "c:\...\appdata\local\programs\python\python37- 
32\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\...\appdata\local\programs\python\python37 
-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\...\AppData\Local\Programs\Python\Python37- 
32\Scripts\project.exe\__main__.py", line 9, in <module>
TypeError: 'module' object is not callable

The thing is that I am not using a __main__.py, plus when I try to look for the file specified by the last path, I cant find anything. Any ideas?

UPDATE 2

I figured a temporary solution, that uses the command sys.exit() when the application is done running. I dont know if it correct, but works for me.

2 Answers

Assuming you are using setup.py packaging, you will need to set the entry_points configuration.

Check out Kenneth Reitz' example repo. He has it commented out, but just uncomment it and transform it to apply to your project.

Packaging Example Repo

Provided you are working within the python environment you installed your project in, this will allow you to just type project [args] into your command line.

Here is the documentation on python packaging: Python Packaging Docs

Example:

Let's say that you have a project with the following structure:

project
  project/
    __init__.py
    cli.py
  setup.py
  README.md
  LICENSE

With a function named main in cli.py that is responsible for the execution of the code.

You would change the commented out code in Kenneth's repo to be:

entry_points={
    'console_scripts': ['project=project.cli:main'],
},

Run modules with cmd using the -m flag:

python -m <package-name>

In your case

python -m project
Related