0

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.

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Apr 11, 2013 at 16:14

2 Answers 2

2

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.

answered Apr 11, 2013 at 16:24
Sign up to request clarification or add additional context in comments.

2 Comments

Sounds like I'll be specifying the full path. I don't think Windows (CMD -> DOS window) supports shebang. I've edited the title to specify that this is a Windows question.
@user2105469: updated to include information about pylauncher, which would make what you want work on Windows too.
1

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.

answered Apr 11, 2013 at 16:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.