Reputation: 11
Using just notepad I typed out:
print ('hello world')
and saved it under ThisPC>windows(C:)>Users>me>Anaconda3
as:
first_program.py
Now I'm in Anaconda Prompt, I typed in Python and got back:
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
But when I try to run the script I thought I made, by typing in (without quotes) "first_program.py" I get back an error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'first_program' is not defined
I have no clue what's causing it. So far I've tried changing the syntax of the script, like:
print 'hello world'
print "hello world"
print (hello world)
print ("hello world")
and other variations with the parentheses and keep getting the same error message.
Please help me understand what I'm doing wrong.
Upvotes: 1
Views: 155
Reputation: 339
When you type in python
in anaconda prompt, it launches the python interpreter
. Any Python code written in this context while be read and executed.
When you type in "first_program.py"
within the context of the interpreter, it will rightly raise a NameError
as in the context of the interpreter, this reference does not exist.
However, if you were to simply type in the python command print('hello world')
and hit enter, the interpreter will render the output correctly and print 'hello world'
on your terminal.
To run the python script, simply open a terminal or Anaconda Prompt (navigate to the directory in which the python script resides) and run your script by typing either python first_program.py
or first_program.py
.
Upvotes: 3
Reputation: 1990
Assuming you're on the command line, and in the same directory as first_program.py
you can do:
python first_program.py
This will start python with your file as what you want it to run and then exit when it is complete.
Upvotes: 1