PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

[FIXED] How to set python scripts launch locations in windows 10

 November 05, 2022     environment-variables, python, windows, windows-10     No comments   

Issue

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?


Solution

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.



Answered By - Kurtis Rader
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing