开发者

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

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 (mos开发者_如何学编程t 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!


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.


To turn a Python module or script into a standalone program on a UNIX system you have to do two things:

1.) Make sure you have the "shebang" in the top of your script:

#!/usr/bin/python

2.) Make sure the script file is executable. This is done using the chmod command:

chmod +x /path/to/helloworld.py

/path/to/ being the fully qualified file path to your script. If it's in the current directory, then you can omit the path.

% ls -l
total 0
drwxr-xr-x  2 jathan jathan   60 2011-04-13 15:28 ./
drwxrwxrwt 12 root   root   6.5K 2011-04-13 15:28 ../
-rw-r--r--  1 jathan jathan    0 2011-04-13 15:28 helloworld.py

It's in my current directory, so let's make it executable!

% chmod +x helloworld.py 
% ls -l                 
drwxr-xr-x  2 jathan jathan   60 2011-04-13 15:28 ./
drwxrwxrwt 12 root   root   6.5K 2011-04-13 15:28 ../
-rwxr-xr-x  1 jathan jathan    0 2011-04-13 15:28 helloworld.py*

See the "x"s in the permission bits on the left? You've done it! Now we can run it:

% ./helloworld.py   
Hello World!

Lastly, never use semicolons as line-endings in Python. It's not required and it's ugly!


Wanted to add my 2 cents: Apart from permissions and path answers above, there is one more situation where you may still face the same error.

In-spite of correct permissions and the shebang header, you may still get the same "Command not found" error if you've originally written the file in Windows and copied it over to Linux. Due to differing line-ending characters, there will be extra '\r' characters on the lines.

This happens because there are non-printable characters in the file. Examing it by doing:

cat -v <filename>:
#!/usr/intel/bin/python^M

The extra "^M" is the problem. Use 'dos2unix' to convert the file and then it'll run fine.


as others said you should chmod +x your file to make it executable and if you don't want to put "./" in your coomand line you should add your current place as system path:

export PATH=$PATH:.


If you're already within python, the syntax to load your script is not helloworld.py :

import helloworld

or

from helloworld import *

you only use the extension .py when you're running python with a script as a command line argument.

No need to apologize, have to start somewhere, and the error messages can be cryptic when you're having basic syntax problems.

Make sure your terminal's current working directory is where your .py file is.


EDITED: try doing /usr/bin/python helloworld.py on commmand line

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜