[Tutor] python: can't open file 'ex1.py' : [Errno 2] No such fileor directory

Alan Gauld alan.gauld at btinternet.com
Wed Aug 25 09:30:59 CEST 2010


"Carter Danforth" <carter.danforth at gmail.com> wrote

> Anyhow, I can't seem to be executing any files in terminal for some
> reason, in this case the file ex1.py:
>
> C:\Users\Carter Danforth\python ex1.py
> python: can't open file 'ex1.py': [Errno 2] No such file or 
> directory
>
> ex1.py is located in "pythonpractice" on my desktop and I've updated 
> the
> modules, here's the output from sys.path:

sys.path (and PYTHONPATH) only affect how imports work within
Python, they have no effect on Windows ability to find scripts.
To run a script you must do one of the following:

1) CD to the script folder and run Python from there as:
      > Python myscript.py

2) From anywhere execute Python as
    > python full\path\to\myscript.py

3) From anywhere execute myscript as
   > myscript.py

For 1,2 PATH must include the Python executable folder
For 3 the .py extension must be associated with the Python
executable and the folder containing myscript must be in PATH.

> I'm not sure why I keep getting this error message and why I'm not 
> able to
> execute any .py files. Any help would be great appreciated.

Windows needs to know where the file lives. You could have many
files called myscript.py in your file system. PYTHONPATH is used
only by Python and only for imports. PATH is used only for 
executables.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list