[python] Python 101: Can't open file: No such file or directory

here's a quick background: I am on day 1 of learning Python. No prior coding experience. Running Windows 8. Python 2.7 installed (location: C:\Python27). I have added the path "C:\Python27\;" to the environment variables and '.py:' to PATHEXT. I am able to launch Python.

To learn, I'm starting with the Google Developers course on Python. I've downloaded the google-python-exercises file to my desktop which has a file 'hello.py'. The Google page(https://developers.google.com/edu/python/set-up) says I should be able to enter 'python hello.py' in the command prompt and it should print 'Hello World'.

When I attempt this, I get the message: python: can't open file 'hello.py: [Errno 2] No such file or directory. How can I fix this?

Update

I'm able to run it by running cmd from the folder. However, this seems to be a temporary solution. How do I program Python to take a new folder (C:\Python27\MyProjects) and search it? It sounds like I need to add/edit a directory, but I'm not sure how to do this.


This question is related to python python-2.7

The answer is


Try uninstalling Python and then install it again, but this time make sure that the option Add Python to Path is marked as checked during the installation process.


From your question, you are running python2.7 and Cygwin.

Python should be installed for windows, which from your question it seems it is. If "which python" prints out /usr/bin/python , then from the bash prompt you are running the cygwin version.

Set the Python Environmental variables appropriately , for instance in my case:

PY_HOME=C:\opt\Python27
PYTHONPATH=C:\opt\Python27;c:\opt\Python27\Lib

In that case run cygwin setup and uninstall everything python. After that run "which pydoc", if it shows

/usr/bin/pydoc

Replace /usr/bin/pydoc with

#! /bin/bash
 /cygdrive/c/WINDOWS/system32/cmd /c %PYTHONHOME%\Scripts\\pydoc.bat

Then add this to $PY_HOME/Scripts/pydoc.bat

rem wrapper for pydoc on Win32
@python c:\opt\Python27\Lib\pydoc.py %*

Now when you type in the cygwin bash prompt you should see:

$ pydoc
 pydoc - the Python documentation tool

 pydoc.py <name> ...
   Show text documentation on something.  <name> 
   may be the name of a Python keyword, topic,
   function, module, or package, or a dotted
   reference to a class or function within a
   module or module in a package.
...

Prior to running python, type cd in the commmand line, and it will tell you the directory you are currently in. When python runs, it can only access files in this directory. hello.py needs to be in this directory, so you can move hello.py from its existing location to this folder as you would move any other file in Windows or you can change directories and run python in the directory hello.py is.

Edit: Python cannot access the files in the subdirectory unless a path to it provided. You can access files in any directory by providing the path. python C:\Python27\Projects\hello.p


I resolved this problem by navigating to C:\Python27\Scripts folder and then run file.py file instead of C:\Python27 folder