When using VSCode with the Python plugin I'm experiencing a very annoying bug, which is known in the community but was hoping to find a workaround.
Basically, when I run a single line execution with VSCode, the terminal enters into python interpreter and executes the line. After this I'm unable to execute the file in the terminal as VSCode doesn't automatically exit the python interpreter but I have to do so manually with quit() or CTRL+D. This can be quite annoying.
>>> /usr/bin/python3 /home/debian/Documents/Tutorial/tutorial.py
File "<stdin>", line 1
/usr/bin/python3 /home/debian/Documents/Tutorial/tutorial.py
^
SyntaxError: invalid syntax
In order to replicate the issue use VSCode in a linux environment and execute a single line of code by pressing Shift+Enter and then try to run the file by pressing the rune file button on the top right.
A fix would be that VSCode would run the single line executions in a seperate terminal, as with PyCharm or have it exit the interpreter prior to executing the file.
So I found two workarounds.
The first one is to run the file without debugging by using CTRL+D
which can still be annoying as it takes a bit longer to load.
The second one is to install the extensions "Code Runner" Code runner lets you run the file in a seperate terminal. And add the following to your settings file to have it working just like PyCharm.
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
"python": "python3"
},
"code-runner.saveFileBeforeRun": true
The question is open for more answers if anyone find a way to get this working by edditing some settings without the use of extensions.
Related
By default, running a .py file through VSCode Code Runner executes the program in the VSCode integrated terminal:
I would like it to do exactly what it does in the integrated terminal, but launch the code through a separate Windows command prompt, like PyCharm.
If I go into settings.json in VSCode, I could change "code-runner.runInTerminal": true, to false, it will execute the code in the output tab instead of terminal tab.
This doesn't help me much.
From settings.json I can change:
to:
This executes the program in the external terminal I want, but it has some serious drawbacks. For one, I have to add input() at the end of my code every time in order for it not to automatically close. The bigger problem is that if I have some kind of logical error in my code, it won't show me any type of error message; just closes the command prompt.
Is there any way to have the code execute in an external Windows command prompt, show all error messages (either in VSCode or terminal itself), and have the terminal require input to close without needing the input() function at the end of my code?
You can open windows terminal/command prompt from the start menu, navigate to your python file and use the same python command to run the python file that you used in VSCode.
[enter image description here][1]
You won't need to add "input()" to your code and you will also get tracebacks.
EDIT: reuploaded picture.
[1]: https://i.stack.imgur.com/qzQz2.png
i have a python file with the following content saved on my machine:
types_of_people = 10
x = f"There are {types_of_people} types of people"
binary = "binary"
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}."
print(x)
print(y)
print(f"i said: {x}")
print(f"I also said: '{y}'")
hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"
print(joke_evaluation.format(hilarious))
w = "This is the left side of ..."
e = "a string with a right side."
print(w + e)
When i open this file with Python 3.7 from within Visual Studio Code i get the following error:
/usr/local/opt/python/bin/python3.7 /Users/andree/Desktop/test.py
File "<stdin>", line 1
/usr/local/opt/python/bin/python3.7 /Users/andree/Desktop/test.py
^
SyntaxError: invalid syntax
In the following screenshot you can see the command i use to run the file and also which python extension i use.
But running the file from within my terminal with python3 test.py works just fine.
Does anyone know what the problem is when running it from within VS Code?
Think this is a bug of VS Code.
When you use "run selection/line in python terminal" command, VS Code starts python interpreter and doesn`t quit it after completion.
You should use exit() command in python interpreter window to end python session.
After that "run python file in terminal" will work fine.
Looks like this is a bug in VS Code.
When i create a new file, assign python language to it and then save it then it works when i run the python file from within the editor.
But when i create a new file, assign python langauge but dont save it, execute "Run Selection/Line in Python Terminal" afterwards save it and then run "Run Python file in Terminal" it doen't work. So this seems to be an VS Code related issue.
The problem for me was that I accidentally used
Shift
+
Return
which executed the python program, when in fact I meant to hit
CTRL
+
Return
to move to the next line without touching the mouse.
Using exit() command in the console worked.
It's a probable bug in VS code. I don't know why there hasn't been a patch for this.
After typing exit() in the terminal, the rerun should work fine. You could also try Ctrl+F5 to run in a debug mode.
Disable terminal.integrated.inheritEnv in settings. This was suggested by VSCode for me and it worked.
I got the same issue, simply restart the Vs-Code it works for me !!
I experienced this issue when attempting to change my default terminal settings. I continually ran into a situation where the "Run Python File in Terminal" command would result in syntax errors while the "Run Selection/Line in Python Terminal" command would error but still display the results. Irritating to say the least.
Here's the settings I utilized to resolve the syntax errors issue.
Note: Enabling Pylint did not resolve my issue, in fact it continued to pop-up even after selecting to enable it. These specific user/workspace/folder settings resolved that issue for me too.
Note: Since the terminal defaults to Powershell, you have to type Python to enter manual commands directly into the python terminal and exit() to close it to allow the python file to run properly again.
USER SETTINGS
{
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
}
WORKSPACE SETTINGS
"settings": {
"terminal.integrated.shell.windows": "C:\\Python3.7.2\\python.exe",
}
FOLDER SETTINGS
"python.linting.pylintEnabled": true,
"python.pythonPath": "C:\\Python3.7.2\\python.exe",
I found a fix for this, install "pylint". I had a pop-up message in Visual Studio that asked me to download this extension. I did and after that I was able to run my code!
I got the same issue, but the code ran for me when I ran it using 'Start without debugging'. This can also be done with the shortcut CTRL + F5.
I found the issue is generated by trailing spaces after the loop functions.
So what I do to alleviate it is add an empty
print ()
statement at the very end of the script
However simple my python script is, I cannot get it running inside VSCODE Terminal by clicking on the "RUN" icon at the top right, whereas it runs fine when called from a CMD window
I had the same problem, this thread helped me I get a syntax error about File "<stdin>", line 1
You are now working inside your Python shell (>>>) while the code you are running (& python "FILENAME.py") is no Python, therefore syntax error.
exit()
to exit the python shell and return to the PowerShell environment.
Simply
python
to enter again, to run python code directly
It seems python is not recognized in your PATH variable. I think the 2 ways to fix are to find your python filepath and adding it your User Variables:
Control panel > System > advanced settings > Advanced tab > Environment Variables
select PATH and then click edit..
in a new line add the path to your python installation (ie C:\Users\YOURUSERNAME\AppData\Local\Programs\Python\Python38-32)
OR, not sure this works out of the box but you could:
install Python module from within VS Code, published by Microsoft
After editing the PATH as suggested by #mah111, I needed to restart VSCODE application
I am using python and Visual Studio code to develop my code. I can run my scripts successfully (Using Run Python File in Terminal). However, this is very inconvenient since after each run I go back to the terminal window and do not have access to python, therefore, I will lose the variables in the previous run so I have three options: 1- Print all the variables I want to screen (This is clearly not scalable). 2- Run the file again. 3- Run the Python file in an interactive window(Ipython).
Option 3 is exactly like running the file in a Jupyter Notebook which is very convenient since you can create a cell below the current cell and keep using the variables after one run.
I would like to know what is the best method to run the python script from the terminal (Pycharm, VS code,...) and can work with variables after (not like every time you forgot to print something you run the file again?
If you set a breakpoint then debug your code (f5), on the bottom of VSCode there is a Debug Console, you can type in debug command there (print(), type(), etc). That seems like it might be what you are looking for.
Try shift + enter on each line this will open the terminal with python
Or try to run .ipynb files instead of .py files in vscode
I am new to Python and currently doing a basic python course to learn. I have been running code all day via the Command Prompt and it has been working fine. For some reason though it has stopped working and python files I try to run are returning the following error:
Traceback (most recent call last):
File "", line 1, in
NameError: name 'hello' is not defined
As per the screen shot and the numbers on it, I performed the following steps in an attempt to run the file:
change to the folder where file is saved
run 'dir' to list all files. I am trying to run 'hello.py'. This contains the code: print('hello simon!')
I can run the file by just typing hello.py into the command prompt, this works ok
I can also run with: python hello.py - this works ok
when I activate Python by typing Python --> Enter, this starts the interpreter ok. However if I then try to run by typing hello.py I get the error message.
This has worked ok all day, I have not changed anything on my PC (to the best of my knowledge!) but just started to get this error a few hours ago. I have looked all over the internet for solution but found nothing. I have uninstalled and re-installed Python, restarted etc... all to no avail.
I am running Python 3.6.5 on a Windows 7 64 bit PC.
It won't let me attach a picture so here is link to screenshot of Command Prompt and error: https://i.stack.imgur.com/BBUe5.jpg
I hope someone can help me with this please
Thankyou
You are not supposed to execute hello.py in the Python Interpreter. It won't work. When you type in python and hit Enter in your Command Prompt, Just type this,
>>> print('hello simon!')
And hit Enter, it would definitely work. Because the interpreter is supposed to execute a code line by line. So if you want to run a Python Script then do not execute it in the Interpreter.
The problem is that when you write python (alone), the command line calls python shell and the following commands are run inside the python shell and not in the command line anymore. Calling a script from the shell has a different format (look it up). You can enter exit() to exit the shell back to command line again
What you are trying to achieve is you are running Hello.py inside Python.
Not with Python.
You need to run Hello.py with Python. As python is interpreter over here.
>>>python
means you are going inside python shell
>>>print('hello simon!')
Is equivalent to your program.
You are running your Python Script as you should and it's working. If you added Python to your path you can run Script you only need to call the Script "hello.py". If you have more than one intepreter, or you didn't added it to your path then you can call it like this "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" you can enven pass arguments to it "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" --argument
When you type python in a shell, then interactive mode is activated. This is like a shell where you type commands and got interpreted right away, in the same way as cmd and powershell works, but for python. This way you can test snippets, or just do simple stuff overly complicated like this
import os
ls = os.listdir(os.path.abspath('c:/'))
def print_dir():
for file in ls:
print(file)
Wich in cmd would be dir c:\ or in powershell ls c:\. The point is that you can test libraries, explore objects, replace the shell or just have fun.