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.
Related
I have tried nearly all solutions in my mind to do this, but the fact is I can't run a python script with modules imported already.
Here's my code for the module cls.py:
import os
def cls():
os.system('cls')
Given below is the code for opening python in cmd:
#echo off
python
pause
What I need is to open the python in the command prompt with the module cls imported. Also, when I try python -m <module> way, it doesn't show any errors, but the program ends.
Any help would be greatly appreciated and thanks in advance.
Saw this question related to mine, but it is not my problem: Run python script that imports modules with batch file
I think what you'r looking for is the interactive mode:
-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command
So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.
Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.
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.
Im working on my Mac and I just fixed the script.
Then I wanted to make an edit and no nothing seem to work anymore.
I get the error message;
Traceback (most recent call last):
File "lu.py", line 1, in
import cplex
ModuleNotFoundError: No module named 'cplex'
I did not made the script and my knowledge about python is not much
When I want to start the script I use
terminal -- cd (file location) pyzton3 lu.py
Anyone who can help me with this problem?
This happens when the Python interpreter cannot find the CPLEX installation.
Usually this can be fixed by setting the PYTHONPATH variable correctly. Make sure that this variable contains the CPLEX Python libraries.
Assuming you are on Linux, you can do the following:
Find the CPLEX (or COS) installation folder.
Assuming this is /path/to/COS then you should have folder /path/to/COS/cplex/python/ and in that folder different sub-directories that correspond to different Python versions.
Assuming you are running Python 3.6, execute the following in your shell: export PYTHONPATH="$PYTHONPATH:/path/to/COS/cplex/python/3.6/x86-64_linux" and then execute your script as before in the same terminal.
UPDATE: Actually, now I have checked, and PyInstaller is saying Invalid Syntax for EVERY script I have, even ones that I have previously packaged with PyInstaller without any issues. I uninstalled and reinstalled PyInstaller, but it's still having the same problem. Is PyInstaller not compatible with Python 3.5.1? That's the only thing I can think of that I might have updated between now and when everything was working fine
Original Question: I'm sure there is a really simple and stupid answer for what I'm doing wrong, because I can't seem to find any other cases of people having this problem.
I have a script I want to package into a standalone executable. In the past, I have used PyInstaller with minimal hassle. Py2exe and cx_freeze have never worked for me. I'm using Python version 3.5.1 and PyInstaller version 3.2, which I believe is the current version since I just uninstalled and reinstalled.
The command I am trying to use is so simple I feel like an idiot for having trouble.
pyinstaller --onefile myscript.py
File "<stdin>", line 1
pyinstaller --onefile myscript.py
SyntaxError: invalid syntax
It's giving a generic SyntaxError: invalid syntax even though that is the exact command straight from the PyInstaller docs.
To be sure, I also tried to include the entire path to my script in the command, added and took out quotation marks, and tried every variation I could think of but it gives me the same syntax error every time.
I'm pretty much a beginner, so any really advanced fixes will go over my head. But like I said, I assume it's something silly I've missed. Thanks in advance.
The syntax error is caused by your command itself, not by the code it calls.
This part is very indicative:
File "<stdin>", line 1
pyinstaller --onefile myscript.py
You actually tried to run that command in a Python shell.
But it is not Python code. You should run it in a usual shell (cli.exe, bash, …)
Run It In CMD
Why are you running it in python shell? It's a problem with python syntax because it is not even defined.
>>> pyinstaller --onefile myscript.py
And, by the way. You are not even importing the PyInstaller module.
Run this line on your CMD:
pyinstaller --onefile filename.py
Ensure your script doesn't has any syntax errors. If it's so, then pyinstaller will rethrow the exception and this could be one of the reason.
I am trying to learn the Stackless python and I got the latest 3.3.6. I created a try.py file and wrote the content
print " Hello world"
print " Good evening"
Then in the python command prompt, gave the command
import try.py
It gave the error ,
Syntax error: invalid syntax as shown in the figure below:
What's wrong here? I have placed the file in C:\Python33.
This is happening because try is a reserved keyword in python.
Try again after renaming your file to something else.
Also, during importing, specifying the extension of the python file is wrong.
Instead of import mymodule.py, you should simply write import mymodule.