Python 101: Can't Open File: No Such File or Directory

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() 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.


6

4 Answers

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.py

2

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

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.
...

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.

1
Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.