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.
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.
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.
Related
I'm running a Conda environment on VSCode and when I Shift-Enter to run main.py , a new Python terminal opens and runs the script, but functions are executing without being called and I'm getting a :
SyntaxError: 'return' outside function
Despite the indentation being seemingly fine.
import math
def h(x):
if x==0:
return 1
else:
return 2*math.sin(x)/x - 1
The above returns:
(base) josh#Joshs-Macbook Coursework1 % source /Users/josh/opt/anaconda3/bin/activate
(base) josh#Joshs-Macbook Coursework1 % conda activate base
(base) josh#Joshs-Macbook Coursework1 % /Users/josh/opt/anaconda3/bin/python
Python 3.8.8 (default, Apr 13 2021, 12:59:45)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> return 2*math.sin(x)/x - 1
File "<stdin>", line 1
SyntaxError: 'return' outside function
>>>
You have executed the command of Run Selection/Line in Python Terminal with the shortcut of Shift+Enter. You can select the command of Run Python File in Terminal which is above it.
Shift+Enter is keybindings for Run Selection/Line in Python Terminal, so if you put cursor in the line of return 2*math.sin(x)/x - 1, it will throw errors:
If you want to enter interactive mode then run the function h(x), please
type python in integrated Termial;
import filename. In my case, it's a.py so i import a;
call the function: a.h(any number)
exit interactive mode;
Or you can get result directly by adding print(h(any number)), then clicking the triangle button in top right corner to run python file in integrated Terminal. Get the same result:
run the entire code in python as opposed to the last line.
if you want to run entire scripts:
python3 xxxx.py
Lets just start by saying I am a complete greenhorn to python and programming altogether but I really do want to learn so if you could try and use terms a beginner would understand that would help me a lot.
OK After getting incredibly frustrated googling how to use python in kali Linux couldn't run a file there either, I downloaded it on My Windows 10 OS then I made a basic script that looks like
#! /usr/bin/python
a = 122
b = 344
print a + b
very simple right. saved it as math.py and went the the cmd prompt (because wikki how told me to) then typed in the location of math py :
cd C:\Users\Mitchel\Documents
I read a question on here that told me to use cd to enter the location. python responded with :
File "<stdin>", line 1
cd C:\Users\Mitchel\Documents
^
SyntaxError: invalid syntax
so I decided to go to try going to the next step anyways and just typed
python math.py
and got the same error. I tried double clicking the file, I tried "open with" and clicking python. I want to know I can type code in notepad or notepad++ and test it before I start because its reall hard to actually write the code in the command prompt itself.
Those are commands to enter at your system's command prompt, but you are entering them in the Python interpreter. Most tutorials assume that you know how to get around your system's terminal. See below for a demonstration.
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\Users\TigerhawkT3>cd
C:\Users\TigerhawkT3
C:\Users\TigerhawkT3>cd ..
C:\Users>py -c "print(1+2)"
3
C:\Users>py
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> cd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'cd' is not defined
>>> cd ..
File "<stdin>", line 1
cd ..
^
SyntaxError: invalid syntax
>>> py -c "print(1+2)"
File "<stdin>", line 1
py -c "print(1+2)"
^
SyntaxError: invalid syntax
>>> print(1+2)
3
>>>
I'm new to Python and I created my first piece of code which is simply
print('Hello World!')
and it works fine and I named it hello.py. When I try to open the file it says with "python hello.py"
I get an error message that says
File "hello.py", line 1
PYthon 3.4.4 (v3.3.3:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1.600 64 bit (AMD65)] on win32
SyntaxError: invalid syntax
I added Python to the PATH so I don't know what's the issue
You're not supposed to put those lines into your script. They come from the interpreter, giving information to you. You're only supposed to include in your script the commands that you want to give to the interpreter. In your case, your hello.py file should consist entirely of this:
print('Hello World!')
and nothing else.
I have been trying for the past two hours to understand what I'm doing wrong. I am just starting off in Python and I can't wrap my head around a few basic things.
I am using:
* Python 2.7.8
* Windows Powershell
This is my error:
>>> python ex1
File "<stdin>", line 1
python ex1
^
SyntaxError: invalid syntax
One thing to note, I've noticed if I force Python to start through a file, it unexpectedly closes. Could this be an administration issue?
I do not know how to do a traceback, or when I try it in Powershell, it gives me an error.
I fixed my silly error: I was using using Python in Powershell, instead of just using the command line. I also made an error in notepad++ - I accidentally had 2 spaces in Line 1 and 2 that I didn't catch.
Thank you for the help!
You are trying to run ex1.py from python shell I think,
Because >>> is prompt for python shell. Please try to do that from command prompt.
I got same error when I tried from python shell.
root#localhost $ python
pythPython 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> python ex1
File "<stdin>", line 1
python ex1
^
SyntaxError: invalid syntax
>>>
Please try
root#localhost $ python ex1.py
I think you should try executing the file in cmd and not in REPL
I was getting the same error but now I tried doing the same on cmd it worked for me!
>>> notepad mybasic.py
File "<stdin>", line 1
notepad mybasic.py
^
SyntaxError: invalid syntax
Do not go inside REPL by typing python + enter rather than work on cmd.
C:\Users\Neha\IIEC_python>notepad mybasics.py
C:\Users\Neha\IIEC_python>python mybasics.py
Hii Neha
I am in the python command line (using python 2.7), and trying to run a Python script. My operating system is Windows 7. I have set my directory to the folder containing all of my scripts, using:
os.chdir("location").
os.getcwd() returns this location.
When I type:
python myscript.py
I get this error:
File "<stdin>", line 1
python myscript.py
^
SyntaxError: invalid syntax.
What have I done wrong?
The first uncommented line of the script I'm trying to run:
from game import GameStateData
It sounds like you're trying to run your script from within Python. That's not how it works. If you want to run myscript.py, you need to do it from a system command prompt, not from inside the Python interpreter. (For instance, by choosing "Command Prompt" from your start menu. I think it's usually under "Accessories" or something like that.) From there you'll need to change to the directory where your scripts are by using the CD command.
Based on the additional information you have provided it does look like you are issuing the command inside of Python.
EDIT: Maybe part of the confusion comes from the term command line. You are at the command line in both the "Windows command" shell, and also when you are inside the "Python shell".
This is what I get in the command line when inside the Python shell:
D:\Users\blabla \Desktop>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> python testit.py
File "<stdin>", line 1
python testit.py
^
SyntaxError: invalid syntax
>>>
Or this:
>>> os.chdir("..")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
My suggestion would be to open a Windows command shell window with the cmd command and then issue your python myscript.py from there.
For more help it would be helpful to see your code, at least the first few lines where the error occurs and some certainty as to where the python command is being issued.
As the other answers indicate, you are probably in the Python shell unintentionally. But if you really do want to run your script from there, try execfile("myscript.py")
on windows shell run echo %PATH%, and check if your .py is under any of the paths.