Disutils Self Extracting Python Package [duplicate] - python

how can I make setup.py file for my own script? I have to make my script global.
(add it to /usr/bin) so I could run it from console just type: scriptName arguments.
OS: Linux.
EDIT:
Now my script is installable, but how can i make it global? So that i could run it from console just name typing.

EDIT: This answer deals only with installing executable scripts into /usr/bin. I assume you have basic knowledge on how setup.py files work.
Create your script and place it in your project like this:
yourprojectdir/
setup.py
scripts/
myscript.sh
In your setup.py file do this:
from setuptools import setup
# you may need setuptools instead of distutils
setup(
# basic stuff here
scripts = [
'scripts/myscript.sh'
]
)
Then type
python setup.py install
Basically that's it. There's a chance that your script will land not exactly in /usr/bin, but in some other directory. If this is the case, type
python setup.py install --help
and search for --install-scripts parameter and friends.

I know that this question is quite old, but just in case, I post how I solved the problem for myself, that was wanting to setup a package for PyPI, that, when installing it with pip, would install it as a system package, not just for Python.
setup(
# rest of setup
console_scripts={
'console_scripts': [
'<app> = <package>.<app>:main'
]
},
)
Details

Related

package_dir in setup.py not working as expected

I'm trying to let users write code as a python module (folder with __init__.py defined) under whatever folder name they see fit. After that I want to install that module as a python package but define the import name myself.
The folder structure would be like this:
project_name/
user_defined_name/
__init__.py
...
setup.py
According to this I should be able to add this to my setup.py to get it working:
setuptools.setup(
package_dir={'my_defined_name': 'user_defined_name'},
packages=['user_defined_name']
)
But the only way that I was able to access the code was by using import user_defined_name. I tried installing the package without -e but that gave the same result. Leaving the packages=['..'] out of the setup functions also did not change the result.
My question is kind of the same as this one and there the only answers seem to be to change folder names and that is something that I would like to avoid. That question mentioned that it might be due to a problem in setuptools but that seemed fixed 3 years ago.
In short, it looks like you need something like that in your setup.py:
setuptools.setup(
package_dir={
'my_defined_name': 'user_defined_name',
},
packages=[
'my_defined_name',
],
)
as Ilia Novoselov said in a comment to your question.
This should work, if you package and install the project normally. You would be able to import my_defined_name.
Although, note that as far as I can tell, this will not work if you use an editable installation (python setup.py develop or python -m pip install --editable .). It will be impossible to import my_defined_name, but you would be able to import user_defined_name, which is not what you want.
#Oliver's answer here clarified this for me.
My TLDR is that to support both releases (python setup.py install and pip install .) and editable installs (python setup.py develop and pip install -e .) you must change your file structure to
project_name
setup.py
user_defined_name
my_defined_name
__init__.py
...
docs
tests
and your setup.py to
setuptools.setup(
package_dir={'': 'user_defined_name'},
packages=['my_defined_name']
)
You can support just releases (NOT editable installs) with
project_name
setup.py
user_defined_name
__init__.py
...
docs
tests
setuptools.setup(
package_dir={'my_defined_name': 'user_defined_name'},
packages=['my_defined_name']
)

how make python command line apps accessible globally?

I wrote a command-line app using python.
the problem is I want the to user can use the command globally after installed the command-line .
I wrote the command-line, I published the package but I don't know how to make this package globally available for users as system commands.
Example :
pip install forosi
and after that user can globally run this command from everywhere they want . like :
forosi help
I'm going to assume you have the main file you are supposed to run in src/forosi.py in your package directory, but you should be able to adapt this if it's different.
First, you want to rename the script to forosi, without the .py extension.
Second, at the top of the file (now called forosi) add the following:
#!/usr/bin/env python3
... rest of file...
In your setup.py for the package, you need to use the scripts option.
setuptools.setup(
...
scripts=['src/forosi'],
...
)
This is the method that required minimal refactoring of your code. If you happen to have a main() function in one of your python files which is the entrypoint of the script, you can just add the following into your setup.py instead of the above:
setup(
...
entry_points = {
'console_scripts': ['src.forosi:main'],
}
...
)
In either case, to build the package locally, run
python3 setup.py bdist_wheel
This will create a wheel file in the dist/ directory called package_name-version-<info>-.whl. This is the official distribution for pypi packages.
To install this package, run:
pip3 install dist/package_name-version-<info>-.whl
or if you only have one version in the dist folder, just
pip3 install dist/*

How do I turn a Python program into a CLI without having to type python3 to execute?

Instead of typing
$ python3 Program.py -<flags> arguments, etc
I want to be able to DL the git clone and then be able to type
$ Program -<flags> arguments, etc
# program name without the .py extension
I've seen other programs have .yaml files, req.txt files and dockerized them but I can't find anything that shows me how to do this. All the tutorials and guides have stopped short of how to make them simple command line programs.
I've done all the argparse, etc but I'm looking for a guide or some instruction of how to dockerize it and simply run it without having to nav to the dest folder
If you're thinking about distributing the program, you should probably add CLI entry points in your package's setup.py file.
For example:
Project structure
ROOT/
- setup.py
- src/
- program.py
src/program.py
# program.py
def main():
pass
setup.py
# setup.py
from setuptools import find_packages, setup
setup(
name='my_program',
version='1.0.0',
packages=find_packages(),
entry_points={
'console_scripts': [
'Program=src.program:main'
]
}
)
The important bit is the line 'Program=src.program:main': it associates the name Program (the name to invoke from the command line) with the function main of src/program.py.
Note that this name could be anything - it doesn't necessarily need to be related to your package name, python file names, etc.
You can perform a local installation of your package in order to test this.
From the ROOT directory, type $ pip install -e . Afterwards, typing
$ Program
in the terminal from any directory will execute the main function from src/program.py.
This behaviour is the same if anyone pip installs your package over PyPI, instead of your local installation.
Add the shebang to the top of the file:
#!/bin/python3 # or wherever your python binary is
If you have that, then you could do:
./Program.py -<flags> arguments etc

setup.py installing package and script - NameError referencing package from script

I'm struggling with my first Python package + script project using setuptools. Here's a skeleton of my setup.py:
setuptools.setup(
name=<PROJECT>,
packages=[<PACKAGE_NAME>],
scripts=['bin/<PACKAGE_NAME>.py'],
python_requires='>=3',
)
My package is in package_name/ and my script, which has the same base name as the package, is in bin/package_name.py. The script does from package_name import *. There are no dependencies.
When I run python3 setup.py install, it succeeds. Thereafter, when I do import package_name in a Python console, it succeeds. But when I run the script from the command line, it fails with a NameError on the first reference to a component from the package. I've reproduced the same error on Mac OS X and Linux.
Why does it fail this way, but doesn't throw an ImportError? How can I fix my script or my setup.py?
has the same base name as the package
That's exactly the problem. When you run package_name.py and the script imports package_name Python tries to import it from that script because it's the first package_name.py in sys.path (Python automatically prepends script's directory to sys.path).
Rename the script to package_name. Or even better create __main__.py in your package and use entry_points:
setup(
…
entry_points={
'console_scripts': [
'package_name = package_name.__main__:main'
]
},
…
)
That way setuptools create the script package_name for you automatically.
See an example in my mimedecode.

running a python package after compiling and uploading to pypicloud server

Folks,
After building and deploying a package called myShtuff to a local pypicloud server, I am able to install it into a separate virtual env.
Everything seems to work, except for the path of the executable...
(venv)[ec2-user#ip-10-0-1-118 ~]$ pip freeze
Fabric==1.10.1
boto==2.38.0
myShtuff==0.1
ecdsa==0.13
paramiko==1.15.2
pycrypto==2.6.1
wsgiref==0.1.2
If I try running the script directly, I get:
(venv)[ec2-user#ip-10-0-1-118 ~]$ myShtuff
-bash: myShtuff: command not found
However, I can run it via:
(venv)[ec2-user#ip-10-0-1-118 ~]$ python /home/ec2-user/venv/lib/python2.7/site-packages/myShtuff/myShtuff.py
..works
Am I making a mistake when building the package? Somewhere in setup.cfg or setup.py?
Thanks!!!
You need a __main__.py in your package, and an entry point defined in setup.py.
See here and here but in short, your __main__.py runs whatever your main functionality is when running your module using python -m, and setuptools can make whatever arbitrary functions you want to run as scripts. You can do either or both. Your __main__.py looks like:
from .stuff import my_main_func
if __name__ == "__main__":
my_main_func()
and in setup.py:
entry_points={
'console_scripts': [
'myShtuffscript = myShtuff.stuff:my_main_func'
]
Here, myShtuffscript is whatever you want the executable to be called, myShtuff the name of your package, stuff the name of file in the package (myShtuff/stuff.py), and my_main_func the name of a function in that file.
You need to define entry_point in your setup.py in order to directly execute something from the command line:
entry_points={
'console_scripts': [
'cursive = cursive.tools.cmd:cursive_command',
],
},
More details can be found here.

Categories