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

2024/11/17 3:47:02

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!

Answer

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.

https://en.xdnf.cn/q/71265.html

Related Q&A

ImportError: cannot import name force_text

I have installed Python 2.7 and Django 1.4 in my CentOS machine and installed all dependencies for my existing project. When I run python manage.py runserver, I am getting the following traceback in my…

How can I select the pixels that fall within a contour in an image represented by a numpy array?

VI have a set of contour points drawn on an image which is stored as a 2D numpy array. The contours are represented by 2 numpy arrays of float values for x and y coordinates each. These coordinates are…

Get the value of specific JSON element in Python

Im new to Python and JSON, so Im sorry if I sound clueless. Im getting the following result from the Google Translate API and want to parse out the value of "translatedText":{"data"…

How does setuptools decide which files to keep for sdist/bdist?

Im working on a Python package that uses namespace_packages and find_packages() like so in setup.py:from setuptools import setup, find_packages setup(name="package",version="1.3.3.7"…

How to implement multivariate linear stochastic gradient descent algorithm in tensorflow?

I started with simple implementation of single variable linear gradient descent but dont know to extend it to multivariate stochastic gradient descent algorithm ?Single variable linear regression impo…

How to do os.execv() in Python in Windows without detaching from the console?

Im using Python 2.6 on Windows 7. I have Windows .cmd file which invokes Python to run the CherryPy Web server (version 3.1.2). I start this .cmd file by executing it at the prompt in a Windows CMD she…

Django queryset permissions

I am building a quite complex Django application to be used on top of and email scanning service. The Django application is written using Python 3.5+This application primarily uses Django Rest Framewor…

How to extract certain parts of a web page in Python

Target web page: http://www.immi.gov.au/skilled/general-skilled-migration/estimated-allocation-times.htmThe section I want to extract:<tr><td>Skilled &ndash; Independent (Residence) sub…

Accessing axis or figure in python ggplot

python ggplot is great, but still new, and I find the need to fallback on traditional matplotlib techniques to modify my plots. But Im not sure how to either pass an axis instance to ggplot, or get one…

Importing the same modules in different files

Supposing I have written a set of classes to be used in a python file and use them in a script (or python code in a different file). Now both the files require a set of modules to be imported. Should t…