43

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.

Max Frai
  • 61,946
  • 78
  • 197
  • 306

2 Answers2

45

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.

Jasiu
  • 2,674
  • 2
  • 27
  • 27
  • Ok, i did that you wrote. But after running: setup.py install i got:
    error: file '/home/ockonal/workspace/scripts/getkey.py' does not exist
    – Max Frai May 17 '09 at 12:52
  • Ok, i've already mady my script installable, but how can i make it global? I can't run it from console just name typing. – Max Frai May 17 '09 at 13:24
  • Where exactly was the script installed? If it's in /usr/bin, then setup.py works fine. Check your script's permissions - it should have +x permissions for everyone. The script in your project should have these persmissions, so that when it's copied to /usr/bin everything will be fine. – Jasiu May 17 '09 at 13:51
  • 1
    Yeah, now my script is in /usr/bin. I tried to do: chmod +x /usr/bin/scriptname.py. But after that there is still: comand not found for my script name in console. – Max Frai May 17 '09 at 13:56
  • 1
    The only other thing that comes to my mind is to check your PATH. Try running your script as /usr/bin/scriptname.py . If that works, but typing scriptname.py doesn't, then it's about PATH. Also, doublecheck that your script has #!/usr/bin/python in the first line. – Jasiu May 17 '09 at 14:24
  • And check permissions of your script in /usr/bin. Is it executable? Is it readable by everyone? – S.Lott May 17 '09 at 18:07
  • 1
    Note in unix-land file suffixes are part of the name. That is, foo.py and foo are different files. The command processor doesn't know of suffixes (that's the whole point of #! ). So if you want people to only have to type 'foo' and not 'foo.py' then name the file 'foo'. – Jay Mar 20 '14 at 17:13
3

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

Community
  • 1
  • 1
Julen
  • 1,024
  • 1
  • 13
  • 29