Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Another option would be to create a batch file for each script you care about, and put the batch file somewhere in your PATH, e.g. create a file called thescript.bat containing...

@echo off
the\path\to\python.exe the\path\to\thescript.py %*

...then you can just run...

thescript arg1 arg2

...which is about as terse a syntax as possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top