user2105469 user2105469 -4 years ago 133
Python Question

PYTHONPATH: command line calls on Windows 7

There are python scripts with command line arguments that I'd like to call from any location on my PC.

The idea is to share the corresponding package with others so they can open up a CMD window and run

python thescript.py arg1 arg2


regardless of their location.

How do I setup the python path/ PATH environment variables?

I've setup a package in
site-packages
, added that path to $PATH and edited PYTHONPATH to include the module directory (which includes
__init__.py
), but CMD won't find the relevant scripts.

python: can't open file 'thescript.py': [Errno 2] No such file or directory


Thanks.

Answer Source

Python does not look up scripts on some sort of path.

You have 2 options:

  • Use the full path:

     python /path/to/thescript.py
    
  • Place the script in a directory that is on your PATH, make it executable (chmod +x thescript.py) and give it a Shebang line:

    #!/bin/env python
    

The second option is probably preferable. On Windows, you can install pylauncher to support shebang lines; if you use Python 3.3 or newer, it is already included with your Python installation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download