1

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.

2
  • please paste the exact contents of your file and the exact command you ran on the command line, using back ticks (under escape key) to format as code Commented Aug 18, 2020 at 23:09
  • 2
    This is simply not how you run a python program. Instead, from a terminal shell, (not the python REPL), use python first_program.py Commented Aug 18, 2020 at 23:14

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hello and thank you for replying! I'm having trouble understanding some of the concepts of your answer. My super basic understanding was that I could create a python file in the command or anaconda prompt and that the .py file I saved would run there. I tried typing in "python first_program.py" into the command prompt and nothing happened, and when I tried using the anaconda command prompt I get error messages.My goal is to create a .py file using text editor and run that file/command/script/whatever using the command and/or anaconda prompt. Is this possible?
Hi, okay so. Whenever you type in "python" in your terminal/anaconda prompt, it will open up the python interpreter which will be denoted by ">>" at the beginning of every line. This means that any python command you type here will be executed by python and the output will be displayed. say, you were to write print("hello world"), python would execute this command and return the output. Now, when you create a python file and save it as first_program.py, you indicate that this is a python executable file and contains python commands. To run it on your terminal,
you will need to ensure that your python path is correctly set in your environment variables. Second, you will have to either be in the same directly to execute this command, or else run the command as python /path/to/first_program.py by specifying the absolute/relative path to your file. Yes, what you are trying to achieve is very much possible. Try running the file in the above mentioned way in anaconda prompt as well. What error messages did you encounter when you ran it again? Was it the NameError again?
I have no clue why this is the case, but it looks like now it's running how I wanted. All I did was type "python first_program.py" and it fed the text "Hello World" like I wanted.
The file path I think was "C:\Users\781bl\Anaconda3" but when I type in "Anaconda3" from "C:\Users\781bl I get an error "(base) C:\Users\781bl>anaconda3 'anaconda3' is not recognized as an internal or external command, operable program or batch file." When I typed "python" first and hit enter, and then typed "first_program.py" it didn't work. I guess they aren't the same thing? I'm glad it's working, but I have no clue why it is now but wasn't earlier, if my actions are the exact same as before?
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.