Getting the syntax error on python interpreter and not on command line

The same code from the same file was executed successfully on the command line.
But the very same code from the python interpreter did not work, gave a syntax error. Im new to python , and im not sure what am i missing. Appreciate any help provided. please find the file with the screenshot

To do what you’re trying to do here, you would:

>>> import Test

If you want to use the print() function, from within the Python shell:

>>> print("Passion for Python")

There’s the operating system console prompt C:\...>, where you type operating system commands, and there’s the Python prompt >>>, when you type Python code. They are not the same. The operating system understands only operating system commands, and Python understands only Python.

1 Like

Thankyou for the response Rob.
your suggestion did work.
this one is not stopping me in moving forward in my learning, but im curious that only once this import works ,after that i try to run the code it just goes to the prompt. i understand that the import works only once , now if i need to run it again, i have to open another console and execute. is there any other way to do this?
im not sure if this question is important. but just good to know this.

Thanks for your support

Thankyou for the response Matthew.
how do i execute the python script file from a python prompt?
is it possible?
if yes, then how?
if no, i understand that executing it from the system console prompt is already working.

You are very welcome.

You can start using the Python interpreter by simply entering python3 at the cmd prompt (to keep things simple, I’ll leave out the full path):

C:\> python3

From there you’ll get to the Python prompt:

>>>

… from which you can enter any valid Python command and have the interpreter execute it (as you’ve already discovered). To exit Python, you enter the command quit()

>>> quit()
C:\>

Now, if you want to run a Python script, one way to do this (yes, there’s more than one way) is to chain the commands, like this, which will run your Test.py script and then exit.:

C:\> python3 Test.py

Passion for Python

C:\>

You may (and should) be using a IDE (Integrated Development Environment), one of which is IDLE, which you can run like this:

C:\> idle

IDLE is a front end for Python and you’ll see that you get the same >>> prompt, but it’s in a window and you’ll have a blue background, rather than black, so you know where you’re at. From there, you can do everything you need to do, but it’s more of a controlled environment and could be better for you to use.

I use Wing as my IDE, and there are many other IDEs, so keep that in mind when you’re reading anything to do with Python.

I hope this helps you.

Python’s interactive shell is not meant for conveniently running scripts. It’s a basic read-eval-print loop (REPL) for interactive work and testing. If you want to run a script and then interact with its main module after the script exits, use the -i command-line option. For example, python -i Test.py.

Alternative interactive shells such as IPython are more capable. For example, in IPython you can run “Test.py” via %run Test. Or run the fully-qualified path of the script: %run "C:/path/to/scripts/Test.py".

Note that using import is not the same as running a file as a script, since the runtime __name__ of the module won’t be “__main__”, and the main module (i.e. sys.modules['__main__']) will be the builtin main module of the interactive shell.

The Python 3.10 distribution for Windows that can be downloaded from python.org includes only “python.exe”, not “python3.exe” or “python3.10.exe”. The distribution from the Microsoft store, on the other hand, supports aliases for “python3” and “python3.10”.

3 Likes

@eryksun

Thanks for that. I’m not a MS Windows user and as such know nothing of those details which, together with your other general notes, adds some good information to this thread.

To be clear, I’m not advocating that script be run by using the >>> import operation (in fact, this is the first time I’ve even seen or tried that) I was just trying to figure out what @PassionWithPythonNewToPython was trying to do. Who knows where he’s picked that up from and I would (and hope I have) discourage that use.

Cheers.


To add: it looks like it’s my bad, as the import was not mentioned before I introduced it.

Got it.
Thankyou so much Rob.
Apologies for wasting your valuable time.

Thankyou for the response Eryk Sun.
Very nice explanation.
Got the point :slight_smile:

No need for any apologies: if you don’t know, you can learn, as we’ve all had to.

One of the things I like about trying to help others, is that, often times, I learn something new myself.

Welcome to the Forum and I hope you enjoy learning Python as much as I do, as frustrating as it can be, especially so when one is just starting the journey.

import is for importing a library package, e.g. import datetime for handling dates. If you ask it to import a second time, it’ll ignore you, because the package has already been imported. It’s not intended for running a program.

1 Like

Got it Matthew.
Thanks .