Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altering my python path: helloworld.py returns command not found—

Tags:

python

Massive apologies for this embarrassing question—

I'm using my MacBook Pro, running snow leopard, and using Python 2.7.1. Trying to run my first script and all the first pages of all my tutorials are laughing at me:

Let me preface with:

$ whereis python  
/usr/bin/python  
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

(Is this my issue?)

I wrote helloworld.py to /users/charles in vim:

$ vim helloworld.py  
#!/usr/bin/python  
# Hello World Python Program  

print "Hello World!";

When trying to run it from terminal:

$ helloworld.py
-bash: helloworld.py: command not found

When trying to run it from python:

$ python
>>> helloworld.py
Traceback (most recent call last):
  File :<stdin>", line 1, in <module>
NameError: name 'helloworld' is not defined

From Dive Into Python (not sure if this is pertinent):

$ python
>>> import sys,os
>>> print 'sys.argv[0] =',sys.argv[0]
sys.argv[0]=
>>> pathname=os.path.dirname(sys.argv[0])
>>> print 'path=',pathname
path=
>>> print 'full path=',os.path.abspath(pathname)
full path= /Users/charles

I'm befuddled! Do I need to alter one of my paths so it finds my script?

I'm absolutely new to programming, I actually just found out that terminal was something you could use.

Thanks!

like image 635
ckemble Avatar asked Apr 13 '11 22:04

ckemble


People also ask

Why my Python file is not opening in CMD?

prompt should be the location of your python file: ex: c:\users\documents\python this is the preferred way. if this doesn't work you may not have added python to path. when initially installing python, you will get an option that says "add to path" make sure you check this.

Why does py work but not Python?

When you installed Python, you didn't check the box to add it to your PATH , which is why it isn't there. In general, it's best to use the Windows Python Launcher, py.exe anyway, so this is no big deal. Just use py for launching consistently, and stuff will just work.

How do I add a directory to a path in Python?

Clicking on the Environment Variables button o​n the bottom right. In the System variables section, selecting the Path variable and clicking on Edit. The next screen will show all the directories that are currently a part of the PATH variable. Clicking on New and entering Python's install directory.


1 Answers

Let's start with the first error you received. Understanding error messages is important.

-bash: helloworld.py: command not found

This indicates that helloworld.py is not a command that can be executed. To run the file, you then have two options:

  1. Run it using the python interpreter. python helloworld.py
  2. Make the file executable and then run it directly. ./helloworld.py

To make files executable in a *nix environment, you have to change their mode to allow execution. In order to do this, you use the chmod command (man chmod for more info).

chmod +x helloworld.py

This assumes that you are in the directory containing the helloworld.py file. If not, cd there first or use the full path.

The ./ is necessary because it tells the shell to run the file located here, not by looking in $PATH. $PATH is a list of possible executable locations. When you try to run helloworld.py directly, the shell tries to look for it in $PATH. You want to run the local file, so you have to prefix it with ./, which means "from here".

As an aside, note the first line of your python script:

#!/usr/bin/python

This is called a shebang line and tells system to use the /usr/bin/python executable to load the file. Internally, that means that the program loader will be doing /user/bin/python helloworld.py.

Finally, when you called python with no arguments, you were dropped into an interactive Python interpreter session. >>> helloworld.py in this environment is not referencing the file of that name, it's just interpreted as python code. Invalid python code. This is why you get your second error, NameError: name 'helloworld' is not defined.

like image 117
Rein Henrichs Avatar answered Sep 28 '22 08:09

Rein Henrichs