I have this error using python 3.8 from the interpreter
>>> python hello.py
File "<stdin>", line 1
python hello.py
^
SyntaxError: invalid syntax
>>>`
I have attempted several changes in the editor using python or env python or python3
what am I missing? Thx in advance
You are trying to run your code while being in the "python shell" A.K.A Python REPL
You need to exit from the shell and try running your code again.
You can quit by typing quit() or by pressing Ctrl-Z on your terminal
Then you can type in python hello.py or preferably python <directory_to_file>/hello.py to run your code.
On side note:
Try looking up a tutorial on YouTube, or reputable websites on learning python.
Try using a popular editor, such as VS Code or PyCharm to enhance your coding experience.
Related
I am getting this error while running a simple code using Visual Studio Code's CodeRunner extension while print values in the same line separated using a space in python.
for i in range(0,6):
print(i, end = ' ')
Error: print(i, end = ' ')
SyntaxError: invalid syntax.
However, the same code runs I select 'Run Python File in Terminal'
Please help me out on this issue and suggest possible alternatives/fixes.
The reason you are getting SyntaxError has to do with your python terminal version being different than the one you've told vscode to use and therefore the one coderunner uses. You can change the vscode python interpreter (version) by using CTRL+SHIFT+P in vscode. This will bring up the Command Pallet where you can type in python select interpreter and select the version of python that matches your terminal version.
If you don't know which version of python your terminal version is that is running the code correctly, go to the terminal and type python --version
I have installed the opensource version of pymol on windows (https://github.com/schrodinger/pymol-open-source).
To run pymol, I can open a python3.8 command prompt on windows and type the following:
import pymol
pymol.finish_launching()
This launches the pymol gui.
I'd now like to create a simple python script so that I can click the script (or convert to a windows executible using for example pyinstaller), however if I put the above commands in a script pymol.py and then run with:
python pymol.py
where python points to python3.8 I get the following error:
AttributeError: partially initialize module 'pymol' has no attribute 'finish_launching' most likely due to a circular import.
Why does this work on the command line python but not in a script and how can I fix this so the script runs and can be converted to a windows executible?
im fairly new to programming in Python and i've been using the terminal to run my code with no problem. Usually by typing python {filename} to run it.
However, recently i got this error:
ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
then i changed the python {filename} to pythonw {filename} and the code worked. Could someone give me some insight into this error and why it worked by just adding w?
pythonw - run python script allowing GUI. According to the man page for pythonw.
I'm a beginner of Python and I want to run a python3 script in PyCharm Python Console, but it raise an error below:
>>> /Users/mymac/Documents/Python/test.py
File "<input>", line1
/Users/mymac/Documents/Python/test.py
^
SyntaxError: invalid syntax
I don't know what's wrong the file path is, how can I solve the problem?
use execfile('/Users/mymac/Documents/Python/test.py'). You are trying to run a python file like an executable. On top of that you are doing it in python interpreter.
Use python's execfile routine to call a file from interpreter or use python test.py to run it from the terminal.
I recognized that the answer I accepted ahead wasn't a perfect method, cause function execfile has been deleted in Python3, the alternative method is using open like:
>>> with open ('/Users/mymac/Documents/Python/test.py', 'r') as f:
... exec(f.read())
...
Still thanks for guys who helped me!
This could be several things. Have you set your environmental correctly? You can test this by cmd: python it should return Python 3.6.4 if that is your current version. If not then head over to tutorialsPoint for how to correctly set up your path.
If that is correctly configured, then have you selected an interpreter in PyCharm. If not, File --> Settings --> Project: Network --> Project Interpreter. Select your python installation path.
Another thing to note is that I suspect you mean to use the terminal instead of the python console.
Then in PyCharms terminal section, python test.py.
I have both 2.7 and 3.0 versions of the Python interpreter installed (on my Ubuntu 32 system), but one particular script uses 3.0.
Using
#!/usr/bin/python3 -B
will not work when the program is run with python myprogram.py.
And I also need a solution that works also in Windows where I also have both python versions installed.
How can I make the script to run only with the right python version?
Please use virtualenv, which makes isolated Python environments easy.
python = Python to use. # This has to be the absolute path to Python executable
os.execl(python, python, * sys.argv)
This way you can restart the script with the python you want to use. Not really stylish.
I don't know why you can't just launch the program with python3 foo.py, but it's possible to have a python2 program relaunch itself as python3 with something like this.
import sys
if sys.version_info.major != 3:
import os
# replace this process with a python3 process
os.execlp("python3", "python3", *sys.argv)
It's a bad solution though, because now your python3 program can't use anything that's not valid python2 syntax
Please take a look at The wrong python interpreter is called
You have to choose a correct interpreter based on where you installed the desired version of Python and your system variables.