I have an existing vanilla Python installed on my Windows 10 computer and I do not want to reinstall Anaconda.
When trying to run code from ipynb file in vscode, I get the following error:
Error: Jupyter cannot be started. Error attempting to locate jupyter:
at A.startServer (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:1:786120)
at async A.ensureServerAndNotebookImpl (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:1:785575)
at async A.ensureServerAndNotebook (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:1:785376)
at async A.submitCode (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:1:782328)
at async A.reexecuteCell (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:75:879318)
Also with the following error from VSCode:
Below are some of the things I have tried:
Check if the VSCode Extension in Correctly Installed
"Jupyter" extensions is deprecated. I had the "Python" plugin from Microsoft installed which contained Jupiter Notebook support.
Jupyter Installed Correctly
I tried reinstalling jupyter:
> python -m pip install --upgrade pip
> pip install jupyter
> pip install notebook
Tried to Run Jupyter on Terminal/Command Line
> jupyter notebook //didn't work
jupyter : The term 'jupyter' 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
+ jupyter
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (jupyter:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
This gave an error not able to find jupyter executable.
According to this post I tried the following and it worked:
> python -m notebook
Check if pointing to the right interpreter
As per this post I tried:
Press Command+Shift+P to open a new command pallete
Type >Python: Select Intepreter to start jupyter notebook server
But I only had one version of Python installed on my computer, and doing this didn't make a difference.
Check the Path
There was this comment about PYTHONPATH in this post but since the python directory is correctly referenced and python works from the command line, I did not investigate it further.
One thing to note is pip installs to my "C:/Users/[username]/appdata/Roaming/Python-38/" folder while my Python is installed in "C:\Program Files\Python38-32\".
If you are having similar issues, please try the above steps mentioned in the question.
After reading this article I realised that I had to map the scripts installed with pip as well, even if it is in my roaming directory. https://discuss.python.org/t/windows-appdata-roaming-vs-local/2682. A confusion that costed me so many hours.
Here are the steps to add the variable:
Go to environmental variables (if you don't know how, here is some instructions: https://www.techjunkie.com/environment-variables-windows-10/)
In the "User variables for [username]" section, Edit "PATH" variable. (it can't be System variables section because only you will have access to your own roaming folder)
Add "C:\Users[username]\AppData\Roaming\Python\Python38\Scripts" (or where the pip installs the scripts to to the PATH variable.
Finally restart VSCode for the new environmental variable to be updated for VSCode.
Now run the scripts in the ipynb file and it should work. It may also complain that it needs other modules, in which case you can use 'pip' to install it.
NB: if you are not constrained by having an existing python version on your computer and not wanting to install more, you can also use the Python Anaconda Distribution. https://www.anaconda.com/distribution/
NB: if you want jupyter note to work for all users using your computer, you need to configure pip to download installs to a directory that is not in your "C:\Users[username]" folder, and add a System variable to it.
For me, another solution helped. I'm not quite sure, what was the issue though, but somehow the state stored for the exact workspace made Python extension crash.
VSCode stores the states for all the workspaces, in its global config folder under /Code/User/workspaceStorage/. See the path to the settings.json in this help paragraph for your OS and then just replace the end of the path. For Windows, for example, the settings path is %APPDATA%\Code\User\settings.json, so the state storage is
%APPDATA%/Code/User/workspaceStorage/
In this directory, there are many subdirectories with some hex names, which are hard to relate to the workspaces. To find out the id of the workspace
Open it in VSCode
Help → Toggle Developer Tools
In Console tab there execute the following to get the workspace id:
vscode.context.configuration()["workspace"]["id"]
Then you can delete the workspaceStorage subfolder by the id of the workspaces.
Another approach is by using the workspaceStorage folder contents themselves. Each of this folder contains a workspace.json which usually includes the path to the workspace. So I wrote a little python script to help me browse them. At the end of the script, there is a draft on removing all the workspaces for the remote containers. Feel free to modify it according to your needs.
from glob import glob
import os, json, sys
from shutil import rmtree
if sys.platform.startswith("win32"): path = os.environ["APPDATA"] + '/Code/User/workspaceStorage/' # Windows
elif sys.platform.startswith("darwin"): path = os.environ["HOME"] + '/Library/Application Support/Code/User/workspaceStorage/' # Mac OS
elif sys.platform.startswith("linux"): path = os.environ["HOME"] + '/.config/Code/User/workspaceStorage/' # Linux
for f in glob(path + "*/*.json"):
with open(f) as fr:
ws = json.load(fr)
d = ""
if "folder" in ws.keys():
d = ws["folder"]
elif "workspace" in ws.keys():
d = ws["workspace"]
elif "configuration" in ws.keys():
d = ws["configuration"]["fsPath"]
ws_path = os.path.dirname(f)
print(d, ws_path)
if d.startswith("vscode-remote://attached-container") or d.startswith("vscode-remote://dev-container"):
print("Inside a container")
# rmtree(ws_path)
Related
I am trying to run a simple command in a .py file on Visual Studio Code, namely:
import os
I get the following return in the terminal:
& : The term 'C:/Users/Tim-S/anaconda3/envs/plotlyenv/python.exe' 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:3
+ & C:/Users/Tim-S/anaconda3/envs/plotlyenv/python.exe "c:/Users/Tim-S/ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:/Users/Tim-S/...yenv/python.exe:String)
[], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Can anyone explain me how I can solve this?
Start by installing Python from python.org or choco. Then, in your terminal (cmd, powershell, windows terminal, etc.), run python. Only then, should you attempt to run anything in VS Code.
You may already be having success up to that point, but it isn't clear from your question. I can see you have some environment (anaconda) installed. There are several VS Code plugins for Python. This answer may help you further with that part.
Here is the Windows port of pyenv, which is an excellent version management tool, which may also be of use.
Are you trying to create a virtual environment: by the looks of the path in the error - 'C:/Users/Tim-S/anaconda3/envs/plotlyenv/python.exe'
seems like you are trying to do so. IF that's the case:
Possible it did not get installed properly and hence cannot find the python file
you have to reinstall it and activate the environment
But that will solve the problem partially
After activating virtual environment, you have to install the library
that you are going to use
What I can see from your problem statement and file path you will need to install os and plotly
pip install os
pip install plotly - if required
I am having an issue with opening and running python on VS Code. In particular, I cannot open any python interpret regardless of having python installed on the computer and the python extension in VS code.
1 week ago, there was no issue with that, I opened VS Code, followed the instructions and python was running (however without any packages or libraries). Because I use python for Data Science projects, I decided to integrate Jupyter notebook - here again, no issues.
Afterwards, I got the idea that I should be able to import libraries and run codes in file_name.py, not only file_other_name.ipynb, so I was trying to create an environment in my project folder that will store installed packages by typing py -3 -m venv .venv .venv\scripts\activate and python -m pip install matplotlib in the terminal. Unfortunately, that did not work when running a basic script - getting a message "Activate.ps1 is not digitally signed. You cannot run this script on the current system."
Hence, I set up the powershell to Set-ExecutionPolicy -ExecutionPolicy Unrestricted. Once, I restarted VS Code, the current environment started showing a message 'Select Python Interpret'. I tried to reinstall the python extension and select from the Command Palette (Ctrl + Shift + P) any of the python interprets, that it is giving me but nothing happens. The message does not change regardless of how many times I have specified which interpret to open. Any idea how I can open the python interpret once again?
enter image description here
There's a default setting called "python.terminal.activateEnvironment": true, so every time you create an virtual environment and select it as your interpreter, don't forget to open a new integrated Terminal(Ctrl+Shift+`) to activate it.
[EDIT]
When you open a new integrated Terminal, there'll be an statement executed automatically: that's a file in your created .venv, which is by red underlined in the following screenshot. Then you get a virtual name prefix before PS, that means you've activated it successfully.
If the Activate.ps1 isn't executed automatically, turn to the .venv folder, copy its absolute path then run it in Terminal still can activate the environment.
Then go on development, like installing the required module:
I know this has been asked before and I reviewed the previous posts, but none of those solved my issue.
I'm new to programming so I may get the terminology mixed up but I will try to explain in as much detail as I can.
I am running Python 3.8 on Visual Studio Code. I installed pipenv successfully:
python -m pip install pipenv
Then I try to actually use it and get this error:
pipenv shell --python 3.8
pipenv : The term 'pipenv' 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
+ pipenv shell --python 3.8
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (pipenv:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
During installation I got this warning:
WARNING: The script f2py.exe is installed in 'C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
However, I don't actually know how to do this or even if I should, because I get this warning with every package I install but it works fine anyway.
I am following this tutorial:
Video Tutorial
and the guy specifically says to run the pipenv commands in the directory where our project is, which I am doing and I get this error. Nevertheless I tried to cd to the directory where the pipenv installed and same error.
I am on Win 10 btw. What am I doing wrong?
You may need to add pipenv to you path variable. Check out the note section in the docs. It actually describes how to get the right location to add on Windows:
On Windows you can find the user base binary directory by running
python -m site --user-site and replacing site-packages with Scripts.
For example, this could return
C:\Users\Username\AppData\Roaming\Python36\site-packages so you would
need to set your PATH to include
C:\Users\Username\AppData\Roaming\Python36\Scripts. You can set your
user PATH permanently in the Control Panel. You may need to log out
for the PATH changes to take effect.
That should get it recognized by your terminal.
The last sentence of that reminds me (it's been a while since I used Windows) -- have you tried to restart or logout? I'm not sure if that's necessary to be honest but I know sometimes it is.
Long story short (and only since you mentioned that you are new to programming) -- the "path" lists all of the locations that are accessible to execute from. You can add to it if you want to be able to execute new programs, like pipenv.
This is what solved the issue for me (Windows 10):
I added the correct Scripts folder of the Python version I am actually running. In my case this was C:\python\Scripts. I added that in Control Panel - search "environment" - Edit environment variables (both for my account and system-wide - did it on both places just to be sure, maybe redundant) - Path - Edit - added the path and now the issue is solved. Thank you!!! –
I just installed python on VS Code and I can't run any python code using python command.
python command:
Running the code seems to run python command by default and it does not recognize it.
When I right click and choose Run Code it complains:
'python' is not recognized as an internal or external command, operable program or batch file
Same goes for manually running python main.py.
When I open an elevated PowerShell and run python, it complains:
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
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (python:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
py command:
It doesn't try to use py command but it recognizes it. And when I manually call py main.py, it works.
When I manually do >py main.py it writes my Goodbye, World!
Question:
How can I make it compile/run in VS Code simply by using the CodeRunner's right-click feature (Run Code)?
I already have both Python folder and its Scripts folder in PATH.
I'm using VS Code 1.27.2 and I have installed python 3.7.0 on my machine and have checked its installer checkbox for adding the environment variables automatically. (PATH is ok)
I also installed : ms-python.python and tht13.python and formulahendry.code-runner extensions on the VS Code.
This is my main.py code:
print("Goodbye, World!")
It turned out that I just had to restart my computer after I installed ms-python.python and tht13.python and formulahendry.code-runner extensions on the VS Code and added python's Scripts folder in PATH.
Now both py and python commands work from anywhere like cmd or Run Code in the right click menu.
Restarting your PC after installing the Python Extension and changing the PATH to include Python and it's scripts folder will help. Worked for me
The Windows installer for Python does not put python on your path by default (there's a checkbox during installation to add it). Make sure that you selected an interpreter that's installed by running Select Interpreter and choosing the interpreter you want (the extension will find them through the registry).
I also had this problem after a fresh Windows reinstallation, vscode didnt recognize the commands like python or pip freeze in the PS terminal.
After reinstalling python and vscode, I read the tutorial for python for vscode: https://code.visualstudio.com/docs/python/python-tutorial. Creating a new venv worked for me py -3 -m venv .venv, then navigate to the venv: .venv\scripts\activate. In the new venv all the python commands worked as normal.
If you have already set the path variable, test the same command in a command prompt and see if it works. If it does, just update PowerShell's path settings by running the following from your vs code PowerShell terminal:
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" +
[System.Environment]::GetEnvironmentVariable("Path","User")`
This trick can save you a lot of restarts.
you need to first confirm if python is installed, for that just run python/python3 on terminal/cmd.
If it runs there and it isn't running in VS Code then restart your system in order to get changes reflected.
And if it doesn't run in terminal/cmd as well then first check if python's directories are placed in environment variables.
Add Python path (ex C:\Users\johndoe\AppData\Local\Programs\Python\Python39) to the %PATH% env variable
I added PATH and did everything. but it didn't work on Vscode Powershell.
but python was working in windows CMD. So I just reinstalled Vscode.
This question might be repeat but I did not get answer.
I have write flowing code in python ide .
out_srs = osr.SpatialReference()
**self.out_srs.ImportFromEPSG(4326)**
It run fine but when i run it from application it cause an error as follows
Note - Error in line enclosed in 2 stars -----
"Unable to load EPSG support gcs.csv file check setting GDAL_DATA environment variable which point to gdal library contains EPSG.csv file"
I have done it but i still get this error. but this code run separately but not in application. This code is from gdal2tile module of gdal. i am using python 2.7.6 and gdal 1.10.0 I am unable to sort out what is the problem and where it is. Please suggest how to solve this.
GDAL needs an environment variable named GDAL_DATA that points to a directory with various data files, including gcs.csv. Learn more about it here.
To check if GDAL_DATA is set, and contains gcs.csv, and if this is readable, use the following snippets to check the application. This should be near the code that raises the error.
import os
import stat
gdal_data = os.environ['GDAL_DATA']
print('is dir: ' + str(os.path.isdir(gdal_data)))
gcs_csv = os.path.join(gdal_data, 'gcs.csv')
print('is file: ' + str(os.path.isfile(gcs_csv)))
st = os.stat(gcs_csv)
print('is readable: ' + str(bool(st.st_mode & stat.S_IRGRP)))
Anaconda / Miniconda users
The correct way to use either Anaconda or Miniconda is to activate an environment where GDAL is installed. For example, activate the base environment for Anaconda from Windows cmd.exe:
call %LOCALAPPDATA%\Continuum\anaconda3\Scripts\activate.bat base
Activating an environment triggers environment variables such as GDAL_DATA (and others) to be set, and often changes the command prompt prefix showing the environment name. These environment variables are unset/restored when the environment is deactivated.
conda deactivate
I was able to solve this issue by taking the following steps to set the GDAL_DATA variable in windows.
Find the folder where gdal data is stored
\Anaconda2\envs\gdaltest\Library\share\gdal
open windows command prompt and run following command with the location of your gdal data folder.
set GDAL_DATA=....\....\Library\share\gdal
Happened to me on MacOS Catalina (10.15.5) while playing with PyQGIS (QGIS 3.12). Just searched on Mac finder for gcs.csv which returned multiple results:
/usr/local/Cellar/gdal/2.4.2_4/share/gdal/gcs.csv
/Library/Frameworks/UnixImageIO.framework/Versions/F/Resources/epsg_csv/gcs.csv
/Library/Frameworks/GDAL.framework/Versions/2.2/Resources/gdal/gcs.csv
I stick with GDAL.Framework and just added this environment variable into my script:
import os
os.environ['GDAL_DATA'] = '/Library/Frameworks/GDAL.framework/Versions/2.2/Resources/gdal/'
Script is not complaining anymore.