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
Related
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
I installed VSCode, downloaded official Python 3.6.4.
VSCode detected and set the environment right - I do see "python.pythonPath" user setting set correctly.
But, when using VS Code using Ctrl+F5to run a Python file, I am always getting asked for "select environment" and it shows me two options
- Python
- Python Experimental
What is this "Python Experimental"? How can I get rid of getting interrupted every time I try to run a script?
Run > Add Configuration... => Choose one of the two options.
After doing this it will no longer prompt you.
Ctrl+F5 is a shortcut to Start without Debugging What you want is just running a file according to your post. To run a python file in the integrated terminal, right-click your editor window and Run Python File in Terminal. It should open up a terminal window within VS Code and run as normal. It also takes into account the environment you are currently using whenever you run it.
To answer your other question about Python Experimental, it is an option to use the experimental debugger, an alternate version of a Python Debugger. You can read the instructions here
I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.
I am using Python 3.3 (2.7 is also installed) and a compatible version of pygame. Recently I have been trying to switch from IDLE to Notepad++
I am using a saved shortcut in Notepad++
C:\Python33\python.bat "$(CURRENT_DIRECTORY)" "$(FILE_NAME)"
which runs the batch file:
#echo off
cd %1
%2
if not errorlevel 1 goto quit
echo.
echo.
pause
:quit
When I run C:\Python33\Foldername\imp_prob.py
import pygame
in IDLE it works fine, in Notepad++ using that shortcut it gives an ImportError: No module named pygame
My questions are:
Why is the NP++ method not producing the same result?
How can I change the shortcut or batch file to make it run stuff that IDLE can run?
What method can I use to ensure that I can import a module regardless of which directory I am running the program from?
edit: a working alternative was in the answers to How do you run a python script from within notepad++?
I had some issues with the code they provided, but replacing "python" with the full path to my python33 install solved that.
I still don't understand why pygame wouldn't import when using my run shortcut. I also don't understand why NppExec works when Run doesn't.
It sounds like you would need to set your systemvariables. Idle does not require these steps. You entered the full path to the python.exe in np++ to execute the python program, but the path to the modules etc. is still unknown.
Add the paths, and try again.
System Properties -> Advanced -> Environment Variables, in the bottom window look for a "Path" variable, Edit and append the following the existing entries (do not delete anything in there!)
;C:\Python33;C:\Python33\DLLs;C:\Python33\Lib
for Python 3.3, if you have installed it into its default directory.
To see if everything worked, open the console anywhere (shift+rightclick -> Open Command Window Here) and just type "python". The python console should open, telling you that you use python 3.3. You then also do not need to tell np++ the full python path, but instead can just use "python" again.
How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?
Wouldn't you simply save your Python code into a file, and then execute that file using Python?
Save your code into a file called Test.py.
And then run it?
$ C:\Python24\Python.exe C:\Temp\Test.py
If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.
If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.
When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.
If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test (if that is a module), or finally from the debugger.
If you put the Python executable (python.exe) on your path, you can invoke your script using python script.py where script.py is the Python file that you want to execute.
Open a command prompt, by pressing Win+R and writing cmd in that , navigate to the script directory , and write : python script.py
A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.
You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables
In DOS you can use edit to create/modify text files, then execute them by typing python [yourfile]