Home > Back-end >  How to set python scripts launch locations in windows 10
How to set python scripts launch locations in windows 10

Time:09-27

I thought it will be as simple as adding these locations to Path or PYTHONPATH. I added them to PYTHONPATH and added PYTHONPATH to Path.

When running SET of window's terminal I can see my newly set paths;

    E:\Tests> SET
    Path=E:\Tests\PythonTests
    PYTHONPATH=E:\Tests\PythonTests

(I simplified the list for readability)

I then create a very simple python file test.py inside E:\Tests\PythonTests with a single line:

    print ("Hello world")

Now, if I cd \Tests\PythonTests I can run it successfully:

    E:\Tests\PythonTests> python test.py
    Hello world    

If I cd \Tests I can:

    E:\Tests> python pythonTests/test.py
    Hello world

But if I try

    E:\Tests> python test.py
    python: can't open file 'test.py': [error 2] No such file or directory

Python version:

    E:\Tests\PythonTests>python --version
    Python 3.8.0

Am I'm missing something? What am I doing wrong?

CodePudding user response:

The PYTHONPATH env var does not control where the python command searches for arbitrary Python programs. It controls where modules/packages are searched for. Google "pythonpath environment variable" for many explanations what the env var does. This is from python --help:

PYTHONPATH   : ':'-separated list of directories prefixed to the
               default module search path.  The result is sys.path.

Specifying a file from which to read the initial Python script is not subject to any env var manipulation. In other words, running python my_prog.py only looks in the CWD for my_prog.py. It does not look at any other directory.

  • Related