Naman Jain
Naman Jain

Reputation: 1

Python script not running from terminal [after being added to PATH]

I am trying to run a python script from CMD (let's say sample.py) and I have added the directory of sample.py to my PATH. When I run "sample.py" in CMD it opens the file in my editor meaning it recognizes it. However, when I run "python sample.py" (python.exe added to path) I get the error:

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

Why does sample.py not run with python but opens otherwise? I am using a Windows machine.

Upvotes: 0

Views: 10913

Answers (2)

guidot
guidot

Reputation: 5333

Adding the directory of the Python file to run to PATH does not help, since it is only used by the operating system to resolve directly executable stuff.

In this case you need to specify the path as in

python path/to/script/script.py

An alternative is to add a special first line into the Python file as in this question and make it directly executable (depending on OS used).

For details you may also want to refer to the corresponding PEP-397.

Upvotes: 1

MarianD
MarianD

Reputation: 14141

You have to add the full path to your python.exe file to the PATH environment variable, not your sample.py file.

Then your command

python sample.py

will launch your script file, supposing you enter that command from the directory containing the sample.py file.

Upvotes: 0

Related Questions