I've installed Python 3.10.5 on my Windows 10 machine. Works splendidly running a file by writing "python file.py". What I can't do though, is to get the extension code runner to run python files through the hotkey ctrl + b. I've reinstalled Python (made sure to check PATH). Rebooted the computer and reinstalled code runner.
If I change no settings I get this error:
"/usr/bin/python3: The term '/usr/bin/python3' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
I found this similar thread: Code Runner does not work for Python in VsCode
After changing the code in settings.json to
"code-runner.executorMap": {
"python": "\"$pythonPath\" $fullFileName",
}
I get the error:
1 | … Data\Local\Programs\Python\Python310\python.exe"
"c:\lekspel\test.py"
| ~~~~~~~~~~~~~~~~~~~~
| Unexpected token '"c:\lekspel\test.py"' in expression or statement.
adding in a "-u" returns this error:
Unexpected token '-u' in expression or statement.
Anyone got a clue how to sort this out?
I solved it by doing a fresh install of vscode, deleting my settings in roaming. Not an optimal solution by any means, but got it out of the way at least.
got the same error. it was because i added "py" to path during installation setup (check by typing "py" in terminal if it opens python) so coderunner wasn't able to run python with "python" keyword, anyway try the following solution and tell me if it works:
1.Go to extensions
2.Right click -> extension settings
3.Edit settings in Json for any option
settings of coderunner
4.change "python -u" to "py -u"
like this
5.save json file with crl+s.
Run code again
tell me if it works for you.
here mine worked like this
Related
I have the following code snippet
import time
print("Printed immediately.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")
In the VS Code: Conda Environment it does not print statement 01 first and statement 02 after 2.4s delay. Instead both are printed at the same time after 2.4s delay.This issue is not visible in the native python version (Python 3.x with Mac OS). How to resolve this issue?
p.s I found out that this error is related with the executing command. The default command which is executed is
conda run -n env_name python "path/test_print.py"
if it is changed as follows it prints with the time delay
python "path/test_print.py"
how to change the default executing command in VS Code?
I think the underlying issue here is that "conda run" buffers stdout until the command completes - see: https://github.com/conda/conda/issues/9412 - but in the mean time I guess the only workaround is to change the executing command. I think the default launch command changed in the most recent (2020.1.57204) VS Code python extension release as I didn't have any such problems before this.
I found that changing the VSCode Python extension setting for condaPath from the default (blank) to something invalid - eg condaX - has the effect of changing the launch command from "conda run -n ...", to "& C:/ProgramData/Anaconda3/envs/tf-gpu/python.exe full_path_to_py_file.py" - which has the effect of running in the appropriate conda environment (tf-gpu in my case) without the IO buffering issue. (I'm running on Windows obviously.)
I had previously set up conda launching using https://medium.com/#udiyosovzon/how-to-activate-conda-environment-in-vs-code-ce599497f20d and I'm not sure if any of those changes had any bearing on the issue but I have since reinstalled the VSCode python extension and can find no trace of those changes, so I doubt any of them are necessary for the condaPath change to work as Ive described.
I'm assuming you don't use any virtual environment in native python, you can do it by:
Open up a terminal in VS Code (or use OS' terminal)
Assuming your default python is linked to python2, type which python3. You will get something like /usr/bin/python3
Open VS Code's user settings in the project folder you in. If there is not, create one: .vscode/settings.json.
Type and save
{
"python.pythonPath": "/usr/bin/python3"
}
Or if you want to change it globally, do it in the settings file -> preferences -> settings and search for python path, and make it /usr/bin/python3
I am well aware of the thread How to execute Python code from within Visual Studio Code
But none of them shows how to get the >>> python shell running with the file imported. Cuz I would like to call functions separately.
I understand that I can get the python shell going by simply typling python in the terminal but the functions in the python file have to manually imported everytime.
Is there a way to run files in VSCode like in IDLE?
you can run the file in an interactive mode in VSC code terminal by using the parameter -i :
python -i py_file.py
Install the Python Extension, then press Ctrl+Shift+P (Command+Shift+P) and type Python: Start REPL. Click the command, and you'll get an interactive Python console.
Assuming you have the official Python extension installed, you can search the command list (Ctrl+Shift+P) for "Python interactive" and you'll see several options that allow to do different variations of this:
Not sure if this was figured out or not, but I was having a similar question. How to simply use something like IDLE (the known '>>>' shell prompt) inside VSCodesee w/o chasing down plugins and their settings? I noticed that no answer was marked confirmed and although insightful, not what I was looking for.
I use Mac and while trying different things to address this, I found that by using/copying the path in the Python Launcher settings I got what I was looking for. Then, I validated the equivalent for Windows. Please see below.
Windows:
If you are using Windows, from your Terminal pane/window in VSCode, enter the path to your python.exe, including the executable name. It is located within the "User > AppData" directory.
For example: C:\Users\YOUR_USER_ID\AppData\Local\Programs\Python\Python310\python.exe
Note that AppData is a hidden folder, so may have to change your view settings in Explorer to show Hidden Items.
MAC:
If you are using MAC, from your Terminal pane/window in VSCode, enter the path to your interpreter
For example: /usr/local/bin/python3
Note: You can also do this directly from a CMD, PowerShell or a Terminal(Mac) window without having to launch IDLE, VSCode or any other coding/scripting app.
To build on the answer already provided, you can automatically run in interactive mode by changing your settings:
Go under File>Preferences>Settings. Search for "arguments" in the search bar. Then under "Python › Terminal: Launch Args", click on the link "Edit in settings.json".
Once settings.json is open, add "-i" with the quotation marks. It should look something like that:
{
"workbench.colorTheme": "Default High Contrast",
"terminal.integrated.localEchoEnabled": "off",
"terminal.integrated.shellIntegration.enabled": false,
"python.terminal.launchArgs": [
"-i"
]
}
Now, it will pass the argument -i every time you run.
I'm trying to run some Python files from command line, using PowerShell, but I'm really unfamiliar with the shell, and I've run into some issues trying to run python. I think there is some preparation process for python that I've missed, but I don't know what.
In trying to run Python through Powershell, I've tried installing Python 2.7 on my computer. While it changed the nature of the error message I was first receiving, it has otherwise not done much.
I've also tried doing some research to see what might be going wrong, personally, but I'm having some trouble finding an exact answer to my problem, and therefore figured I'd try asking, on here.
From what I've seen, online, I think this is the correct way to call and run a python file in Powershell. It's what I'm currently using:
PS C:\Users\nickd\documents\Coding\Comp_Sci_Class\Python\debugger> python my_test.py
This should run a program that tests whether or not a debugger is working. Instead, I get the following error message when I try and use the command 'python':
python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ python my_test.py
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (python:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
As such, I'm led to believe that there is some step in preparing Python for use on Windows Powershell that I'm just missing, but I'm not finding a clean cut answer for what that might be.
Could somebody please help me out, and describe what it is I might need to do? I know that what I'm missing is likely extremely simple, and I apologize upfront if this is a silly problem to have. That said, if somebody could point me in the right direction as to what I need to do, I'd be really grateful.
Thank you.
Powershell doesn't find where you have installed python. If you add the folder where python.exe is located to path then it will work.
See here https://superuser.com/a/143121
You have to add location of Python.exe to your environment variable..
Your path might be C:\Program Files\Python27
Please follow the below steps :
1) Right Click on This PC (My Computer) and Select Properties ( Win Key + Pause key)
2) Click on Advance system setting from left pane
3) Click on Environment Variables...
4) select variable called "Path" from second list box
5) Click on Edit button below the same list box
6) Add python.exe full path ( do not include filename )
7) close all dialogs by clicking OK
8) restart your powershell | ISE
Since Python 3.3, the PythonLauncher is installed with Python on Windows. It is py.exe and is already in the PATH (c:\windows) if you did an "all users" installation. The default installation of Python also registers py.exe as the default handler for .py files.
Normally, with py.exe registered as the default handler, mytest.py would be sufficient to run the script, but since Powershell doesn't search the current directory by default, you can use .\my_test.py if it is in the current directory.
Otherwise, just run py my_test.py (if "all users" installation) or add the user directory where py.exe was installed to PATH if not. If you have multiple versions of Python installed, py -3.6 (for example) will run Python 3.6.
More information:
Python Launcher for Windows
Using Python on Windows
I'm trying to switch from Notepad++ to Atom, but I just can't manage to get my scripts executed in Atom.
I followed this answer (so I already installed script) which is not really extensive and also the rest on the web doesn't offer anything comprehensible for beginners.
In Notepad++ NPPexec I used to
NPP_SAVE
cd "$(FULL_CURRENT_PATH)"
C:\Python34\python.exe -u "$(FULL_CURRENT_PATH)"
and in Sublime Text 2 I made it run by creating a new "Build System":
{
"cmd": ["C:\\python34\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Can you please guide me how to setup Atom to be able to execute Python scripts with Python 3.4 scripts with a keyboard short-cut?
I already tried to set my init-script to:
process.env.path = ["C:\Python34\python.exe",process.env.PATH].join(";")
respectively
process.env.path = ["C:\Python34",process.env.PATH].join(";")
with no success.
When I go to Packages -> Script -> Configure Script and type
C:\\Python34\\python.exe
it works. But thats not a permanent solution.
When I press Ctrl+Shift+B to run a script, without configuring it before (as it is supposed to work), I get (suggestion of ig0774's comment implemented):
(it doesn't matter whether it is C:\Python34 or C:\Python34\)
It complains that python is not in my path - but it is.
I read multiple times that Windows 7/8 64bit together with Python 3.x could cause issues with certain packages. May this be the reason in ths case as well? I have Windows 7 Pro x64.
Update
As I've switched to VSCode and probably stay there, I'm not willing/don't have the time to try out all the answers, so I let the community judge the answers and accept always the highest voted. Please ping me, if it's not correct anymore.
This can be easily solved by editing the /home/.atom/packages/script/lib/grammars.coffee file (note that the atom folder is hidden so you might have to press ctrl+H to view hidden files and folders)
Inside grammars.coffee find:
Python:
"Selection Based":
command: "python"
args: (context) -> ['-u', '-c', context.getCode()]
"File Based":
command: "python"
args: (context) -> ['-u', context.filepath]
and replace with:
Python:
"Selection Based":
command: "python3"
args: (context) -> ['-u', '-c', context.getCode()]
"File Based":
command: "python3"
args: (context) -> ['-u', context.filepath]
Save changes, restart Atom and enjoy running your scripts with python 3
EDIT: On Windows I believe the grammars.coffee file is located in
C:/Users/Your_Username/AppData/Local/atom/packages
Again, the AppData folder is hidden so you might have to change your settings to view hidden files and folders.
To expand on #matt-nona answer. You can go to his mentioned config file right from Atom. Simply go to settings then "Open Config Folder":
Then /packages/script/lib/grammars.coffee Find "Python" and make the appropriate change to python3:
Following up on Matt Nona's advice , when Atom starts-> Welcome Guide (or control+shift+T)-> 5th one down 'Hack on the Init Script'. A blank page will open and you can add that modifications in there.
Update: for any other souls looking for this answer - On my Mac I do not have a grammars.coffee file within atom script config file.
Instead, there s a grammars folder, and I have a python.coffee file in there. The same changes outlines in the screenshot (ie add '3' to the end of the two mentions of python) fixed my issue and atom automatically runs Python3 now.
Not sure if the above answers are Windows specific or if there have been dev changes since 2017.
same problem just like you. 'Packages -> Script -> Configure Script' is not permanent. So I has tryed another script runner:https://atom.io/packages/atom-runner, just found the problem is in the python script itself.
When I use atom-runner, I got error message like this:
atom-runner error
So it remind me that in the beginning of the python script: ' #!/usr/bin/env python3 '
It's obvious that the ENV_PATH is WRONG here. I should revise it in my python script.
Setting the PATH within Atom did not work, setting it with the cmd, via
set PATH=%PATH%;C:\Python34
neither, and setting it in the Windows 7 system properties failed as well.
However reinstalling Python 3.4 and check Add python.exe to Path
seems to be neccesary. Also I needed to uninstall Atom completely (inculding all packages or a least script) and reinstall it from scratch.
After all these steps:
Install Python with Add to Path
Install Atom
Install script package
it works out of the box (Ctrl+Shift+B) and no further steps are required.
I still don't know what was the reason before and I don't know which of this steps are really required. So feel free to include your procedure without reinstalling everything.
Update
Reinstalling everything is certainly not necessary, simply updating/repairing the installation with the installer is sufficient.
Use the script-runner https://atom.io/packages/script-runner/
"N.B. these keyboard shortcuts are currently being reviewed, input is welcome.
Command Mac OS X Linux/Windows
Run: Script ctrl-x alt-x
Run: Terminate ctrl-c alt-c"
And "Run Terminate" (Alt + c) to use the current python in your system.
edit your python.coffee script
$ sudo nano .atom/packages/script/lib/grammars/python.coffee
For Linux and Mac, adding environment in the script will pick correct python version. (command + I to run)
for running with python3
#!/usr/bin/env python3
This is a weird bug. I know it's something funky going on with my PATH variable, but no idea how to fix it.
If I have a script C:\Test\test.py and I execute it from within IDLE, it works fine. If I open up Command Prompt using Run>>cmd.exe and navigate manually it works fine. But if I use Windows 7's convenient Right Click on folder >> Command Prompt Here then type test.py it fails with import errors.
I also cannot just type "python" to reach a python shell session if I use the latter method above.
Any ideas?
Edit: printing the python path for the command prompt that works yields the correct paths. Printing it on the non-working "Command prompt here" yields: Environment variable python not defined".
First of all, I work on Windows7 (among others) and running python from the command line works for me using "Command Prompt Here". Make sure you have the directory containing python.exe in your PATH environment variable, by running "Command Prompt Here" and running set.
Now for import errors. When importing, Python looks for modules in directories specified in the sys.path list. The PYTHONPATH environment variable is added to this list, along with some default directories, and the directory of the given Python script. However, in IDLE this directory is the directory of IDLE, so this could be causing the difference you are seeing when running things from IDLE compared to running them from the command line.
See http://docs.python.org/tutorial/modules.html#the-module-search-path for details.
Here is my advice on how to resolve this issue. You didn't mention what import errors you are recieving, but try running the script inside IDLE and checking the problematic modules' .__file__ attribute to see where they are. Then compare the sys.path from inside IDLE to sys.path you get when running Python from the command line. This should give you the information required to resolve your import errors.
I don't use Windows much, but maybe when you open Right Click -> Command Prompt, the PATH is different from navigate manually. First try to print your PATH (oh I have no ideal how to do this) and see if it different in 2 situation.
You can check the currently present enviroment variables with the "set" command on the command line. For python to work you need at least PYTHONPATH pointing to your python libs and the path to python.exe should be included in your PATH variable.