Changing my python path: helloworld.py returns a command not found -

Massive apologies for this awkward question -

I am 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:

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

(It is my problem?)

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

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

When trying to start it from the 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 immersion in Python (not sure if this is appropriate):

 $ 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 confused! Do I need to change one of my paths to find my script?

I am completely new to programming, I actually just found out that the terminal was what you could use.

Thanks!

+7
source share
6 answers

To include a Python module or script in a standalone program on a UNIX system, you need to do two things:

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

 #!/usr/bin/python 

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

 chmod +x /path/to/helloworld.py 

/path/to/ is the full path to your script file. If it is in the current directory, 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 

This is in my current directory, so let it become 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" in the resolution bits on the left? You did it! Now we can run it:

 % ./helloworld.py Hello World! 

Finally, never use semicolons as line endings in Python. It is not required, and it is ugly!

+7
source

Start with the first error you received. It is important to understand the error messages.

 -bash: helloworld.py: command not found 

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

  • Run it using the python interpreter. python helloworld.py
  • Make an executable file and run it directly. ./helloworld.py

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

 chmod +x helloworld.py 

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

./ necessary because it tells the shell to run the file located here, rather than looking in $PATH . $PATH - a list of possible executable places. When you try to run helloworld.py directly, the shell tries to find it in $PATH . You want to run a local file, so you need to attach it to ./ , which means "from here."

As a note, pay attention to the first line of your python script:

 #!/usr/bin/python 

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

Finally, when you called python with no arguments, you were deleted in an interactive Python interpreter session. >>> helloworld.py in this environment does not refer to a file of this name, it is simply interpreted as python code. Invalid python code. That's why you get your second error, NameError: name 'helloworld' is not defined .

+11
source

as others said you must 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 location as the system path:

 export PATH=$PATH:. 
+1
source

I wanted to add my 2 cents: in addition to permissions and answers on the way above, there is another situation where you can still encounter the same error.

Despite the correct permissions and the shebang header, you can still get the same "Command not found" error if you originally wrote the file to Windows and copied it to Linux. Due to the different line ending characters, there will be additional characters '\ r' in the lines.

This is because there are non-printable characters in the file. Studying this by doing:

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

The problem is "^ M". Use 'dos2unix' to convert the file, and then it will work fine.

+1
source

Editorial: try to make /usr/bin/python helloworld.py through the communication line

0
source

If you are already in python, the syntax for loading your script is not helloworld.py:

import helloworld

or

from import helloworld *

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

No need to apologize, you need to start with something, and error messages can be cryptic if you have problems with the basic syntax.

Make sure that the current working directory of your terminal is located where your .py file is located.

0
source

Source: https://habr.com/ru/post/885868/


All Articles