0

When installing django via pip, the PATH environment variable is modified to let the user access django-admin directly in the terminal. (django-admin startproject project_name)

I would like to the same for my pip package. I looked at how modifying environment variable using python but os.environ["PATH"] wont get saved once the python script is ended.

Any suggestion ?

AlixL
  • 372
  • 2
  • 13
  • Do you mean you want to add modules that you installed with pip to PATH? – sP_ Aug 09 '18 at 21:35
  • @sP_ when entering "pip install foo" I would like to add at the same time foo-script.py to be accessible via the terminal by simply entering "foo-script" – AlixL Aug 09 '18 at 21:43
  • 2
    http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point – abarnert Aug 09 '18 at 21:49
  • This is probably a dup; if not, I can write an answer. But either way, the answer is just going to quote from and link to that page. – abarnert Aug 09 '18 at 21:49
  • try this once https://stackoverflow.com/a/5458250/6680829 – Asif Mohammed Aug 09 '18 at 21:50
  • @AsifMohammed No, that's a completely different thing. – abarnert Aug 09 '18 at 21:50
  • isn't that making python program globally accessible ?? @abarnert – Asif Mohammed Aug 09 '18 at 21:51
  • sorry. i have misunderstood @abarnert – Asif Mohammed Aug 09 '18 at 22:03
  • 1
    @AsifMohammed That's about creating a standalone executable that doesn't require a Python installation, which the OP hasn't asked for. And, after doing that, the OP would still need to create an installer to install that executable and get it onto the user's PATH, just as they already need to do for their Python script—except that now they'd have to build an installer/rpm/etc. for each platform instead of just relying on `pip`. – abarnert Aug 09 '18 at 22:07

1 Answers1

1

The ideal way to do this is to give setuptools an Entry Point.

Instead of writing a script, you write a function in one of your modules.

For example, add this line to your setup call in setup.py:

entry_points = {
    'console_scripts': ['mytool'=mypkg.myscript:myfunc'],
}

… and setuptools will, at install time, automatically create a script named mytool that imports mypkg.myscriptand calls itsmyfunc, all in a way that's appropriate for whatever Python installation the user used topip installyour package, and putmytoolonto the user'sPATH`.

This way, it takes care of all kinds of portability issues you never even thought of. What if the user is on Windows and their default Python is 2.7 but they used py -3 -m pip to install it? They'll get a file named mytool.py instead of mytool, and it'll have a Windows py-launcher-compatible shbang line, and it'll just work.


Occasionally, you can't reorganize things that way, and you have to manually write a script. In that case, just give setuptools the path to the script, by adding this argument to your setup call:

scripts=['bin/mytool']

setuptools can't do all of its magic here, but it can still get the script installed into the user's PATH.


Of course all of this depends on Python being set up properly. But however Python managed to put pip and django-admin on your PATH, then it will put your script on the PATH in the exact same way.

abarnert
  • 354,177
  • 51
  • 601
  • 671