VSCODE no longer detecting python virtual environment - python

On Windows.
Normally, when I run
python -m venv myvenv
In the bottom right corner, VSCODE will detect this virtual environment and prompt me to set it as default interpreter.
In fact, this was working up to about 30 minutes ago.
However, I was having errors with path in Git Bash and followed instructions here to change my default terminal back to the CMD.
After doing so, VSCODE no longer recognizes any virtual environment I create automatically. Doing CTRL SHIFT P and trying to set it manually doesn't work either, as it cannot seem to find it.
However, if I do
source myvenv/Scripts/activate
It does indeed open the virtual environment. However, this only lasts for that one terminal session.
How can I fix this problem?

The crisp of VS code Environment setting as per there site:
From within VS Code, select a Python 3 interpreter by opening the Command Palette (Ctrl+Shift+P), start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too)
The command presents a list of available interpreters that VS Code can find automatically, including virtual environments. If you don't see the desired interpreter, see https://code.visualstudio.com/docs/python/environments
Note: When using an Anaconda distribution, the correct interpreter should have the suffix ('base':conda), for example Python 3.7.3 64-bit ('base':conda).
Selecting an interpreter sets the python.pythonPath value in your workspace settings to the path of the interpreter. To see the setting, select File > Preferences > Settings (Code > Preferences > Settings on macOS), then select the Workspace Settings tab.
Note: If you select an interpreter without a workspace folder open, VS Code sets python.pythonPath in your user settings instead, which sets the default interpreter for VS Code in general. The user setting makes sure you always have a default interpreter for Python projects. The workspace settings let you override the user setting.
Above Paragraph is taken from: https://code.visualstudio.com/docs/python/python-tutorial#_select-a-python-interpreter

Try the solution from the below link and do let me know in the comments if that works for you
Even though I have referred to the exact section of the page required for your resolution, as suggested by #Ch3steR, I am putting the required details below :-
From within VS Code, select a Python 3 interpreter by opening the Command Palette (⇧⌘P), start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too):
No interpreter selected
The command presents a list of available interpreters that VS Code can find automatically, including virtual environments. If you don't see the desired interpreter, configure your Python environment.
for setting python environment, please follow below instructions: -
To select a specific environment, use the Python: Select Interpreter command from the Command Palette (⇧⌘P).
Python: Select Interpreter command
You can switch environments at any time; switching environments helps you test different parts of your project with different interpreters or library versions as needed.
The Python: Select Interpreter command displays a list of available global environments, conda environments, and virtual environments.
Note: When using an Anaconda distribution, the correct interpreter should have the suffix ('base':conda), for example Python 3.7.3 64-bit ('base':conda).
Selecting an interpreter sets the python.pythonPath value in your workspace settings to the path of the interpreter. To see the setting, select File > Preferences > Settings (Code > Preferences > Settings on macOS), then select the Workspace Settings tab.
Reference :-
https://code.visualstudio.com/docs/python/python-tutorial#_select-a-python-interpreter
https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment
Thanks

Related

VS Code does not change python environment

I am using VS-Code and anaconda environment for python interpreter. I select the exact anaconda base environment by ctrl + shift + ` and it also reflects in the downside panel of vscode. But, when I checked the python version it shows my system's default python environment 3.7.9. If you see the below screenshot than, the anaconda environment is with 3.8.3.
Please give me solution, Thank you.
Changing the version in VSCode does not change the the instance that your PS instance will use. Try doing where python to see where the V3.7.9 that your PS instance is picking up is. Then remove that version from the environment variables and add the path to the V3.8.3 instead.
Additionally you can do: To forcefully use v3.8.3
Specify python version in command
py -3.8 <command>
OR set PY_PYTHON environment variable to set which version to use.
Take a look at this for further help Python docs
after changing the environment, you can restart the vs code again. it might be changed now. if not, then try changing now againg by clicking the interpreter name which is displayed on left bottom of the vscode window
For those tried these steps and achieved nothing:
select different interpreter
reboot VScode
reinstall VScode Python extension and delete its folders
Probably you are working in the workspace and not in folder. You probably set interpreter at workspace level, that can't be used in one of the folders of the workspace. Try to open your folder separately from the workspace and select interpreter you want. This worked for me.
To check & change vs code interpreter:
In top left menu bar Click view
In the dropdown menu, Click Command Palette
Click Python: Select Interpreter
Choose & Click on your desired Interpreter
Another way to be sure to use anconda interpreter, open anaconda navigator and launch vs code from there.
original vs code How-To

PyCharm not able to create conda environment

I'm pretty new to programming, so maybe there's something painfully obvious that I've missed, but I've searched a lot around for solutions to this issue without finding any.
I just got a new computer and installed Anaconda and PyCharm for Anaconda. I got it to work fine on my old computer, but when I try to open PyCharm now and create a project, it gets stuck on the creation of the conda environment. Apparently there's a new conda update, so I tried to update to this one in Spyder, which seemed to work fine, but when PyCharm tries creating a new environment, it gets stuck and only displays
"Creating Conda environment"
"$ conda update -n base -c defaults conda"
Does anybody know what's happening? I've tried just waiting it out, but it doesn't seem to be moving forward at all from that point. I've also uninstalled both Anaconda and PyCharm with all their plugins and reinstalling them, but to no avail.
Thanks for any help or input!
Sincerely, confused first year student
It appears that Sophie was able to get the conda environment up and running.
However, other newcomers might come across a similar problem with regards to setting up Anaconda for use within PyCharm. Therefore, I provide a guide below that I recently (today) used in order to accomplish this on a fresh install on a new computer. For this I decided to use PyCharm Community Edition 2018.3 and Anaconda3 version 2019.07. I performed this on Windows 10 Enterprise version 1903. I have also performed this procedure on Windows 7 Home Premium edition (some time ago).
This will enable you to use PyCharm and Anaconda together, so that you can:
Know the prerequisite steps leading up to creating a conda environment, and become familiar with the steps following this action with using Anaconda with PyCharm.
Use the Terminal window in PyCharm with packages that are installed with Anaconda's version of Python. You can then use both regular python and conda commands to view, update or install packages. For instance, depending on your preference, you could use 'pip list' (python command) or 'conda list' to list installed packages.
Use the Anaconda python interpreter as the default Python Console in PyCharm
To accomplish this, please follow these steps:
Install PyCharm, and import any settings file you may have from previous PyCharm installation on other computer (optional). Assuming you are starting fresh, create a new project and set the (default) project folder location.
Install Anaconda. My install location was set to C:\ProgramData\Anaconda3. Note that 'ProgramData' folder is hidden by default. You should enable viewing of hidden folders in windows explorer, if you need to manually browse for it. Upon installation, I decided to have the options Add Anaconda to my PATH environment variable and Register Anaconda as my default Python 3.7 set as deselected.
Open Windows Explorer and paste in C:\ProgramData\Anaconda3 (or your install directory) in the address bar. This should take you to that directory. Navigate to the environments folder named envs. Select the address bar and copy that address. I got C:\ProgramData\Anaconda3\envs. You will need this later.
Open program named Anaconda Navigator (Anaconda3). You should see this from the list of newly installed programs. Alternatively, search for it and open it. This will initialize some stuff (unknown to me) that I have found necessary to be able to complete this guide. For the next steps we dont need this program running. You could close it if you want.
Decide on a name for the (ana)conda environment that we will create. I used py37 for simplicity. You only need to do this once for the python projects you will make. It is possible to make more environments if you should need that for some python projects in the future.
Open program named Anaconda Prompt (Anaconda3), preferentially as administrator. Opening as administrator may prevent complications that could occur on some systems. To do so, search for the program and then right-click it, and choose Run as administrator.
Enter this command: conda create -n py37 python=3.7 anaconda. It should prompt you to install some packages. Enter y to accept and proceed with the setup. This step may take some time to finish. Once finished, it may show you the needed command to activate the environment. Don't activate it just yet, ie. ignore that message for now.
Assuming environment path C:\ProgramData\Anaconda3\envs and environment name py37, run the following command (alter as needed to match your environment path and name): set PATH=C:\ProgramData\Anaconda\envs\py37\Scripts;C:\ProgramData\Anaconda\envs\py37;%PATH%
Run this command: conda activate py37. This will activate your environment.
Navigate to a folder you can easily access, such as the Documents folder on your machine. To do so, execute this command: cd "C:\Users\YourUsername\Documents".
Execute the following command: echo %PATH% > path_value.txt. This will export "PATH" values to a text file that can be found in the Documents folder as path_value.txt. Open this file and copy the content within. This will be needed in the following steps with setting up PyCharm for use with the Anaconda environment.
In PyCharm, navigate to Settings - Tools - Terminal. Check that Start directory field contains the path of your project folder, ie. such as C:/Users/YourUsername/Documents/YourProjectFolderName. Then, select the folder icon for the Environment variables field. Click the plus symbol, and add a new entry with PATH and your path value (as found from the path_value.txt file) in the Name and Value fields, respectively.
In PyCharm, navigate to Settings - Project Interpreter - Add Python Interpreter - Conda Environment - Existing environment. Browse for C:\ProgramData\Anaconda3 (or your install directory). Select the file named python.exe. I chose to edit the Name field of this Anaconda python interpreter as Python 3.7 Anaconda.
In PyCharm, navigate to Settings - Build, Execution, Deployment - Console - Python Console. Check that the Python interpreter field contains Project Default (Python 3.7 Anaconda) or similar entry. The Environment variables input may be empty. Select the folder button next to it and add a new entry with PATH and your path value (as found from the path_value.txt file) in the Name and Value fields, respectively. Exactly the same entries as was inputted in step 12.
[Optional] In PyCharm, navigate to Settings and search for the Run context configuration option. Right click it and select a desired shortcut method. Personally I use a keyboard shortcut which is set to Ctrl+Shift+Less. Once the rest of these steps are followed, this will allow you to run the current python .py file you have open, without having to manually set up configuration options for it (through Add Configuration option, next to green play button, as seen if you have no configurations set up from before). It will force PyCharm to use the default project interpreter (which now is the desired Anaconda-delivered Python 3.7 installation) for whatever project you have open. To use it, just open a .py python file with some code inside, click anywhere inside of it, and then use your shortcut option.
[Note: If you skipped step 15, go to step 17] Run your python file with the method from step 15. This should automatically set up a run configuration option for that file. You may get an error of missing packages. Ignore that for now.
Select Edit Configuration or Add Configuration in the dropdown box next to the green play / run button. Open the Templates tree, then select Python. Check that the Python interpreter option contains Python 3.7 Anaconda or similar. The Environment variables field may contain PYTHONUNBUFFERED=1. Regardless, select the related folder button and add a new entry with PATH and your path value (as found from the path_value.txt file) in the Name and Value fields, respectively. Exactly the same as we did in steps 12 and 14.
Restart PyCharm to re-initialize the Terminal and Python Console windows. Or manually close and reopen those windows.
You have now created and activated an (ana)conda environment, and enabled it's use from within PyCharm. You should also now be able to ie. copy your code over to a fresh new file, and then hit ie. Ctrl+Shift+Less to run the file without having to manually set a configuration file for it.
Hope this helped!
Sources: source1, source2, source3
In my Experience I just opened PyCharm as administrator and the error was gone!
If you use PyCharm to create conda environment, you can choose conda environment not virtualenv.
When you get the error like "can't get the /path/to/", you can open PyCharm with administrator.
If you use "anaconda prompt" to create conda environment, you can use command line like conda create -n py36 python=3.6. Then you can activate environment using conda activate py36.

VSCode / Virtual Studio Code: Unable to Load Conda Environment in VSCode Terminal

I'm attempting to switch to VScode, to use the built-in terminal to interact with Anaconda, and know VScode supports Anaconda because it ships with Anaconda.
From the documentation, and various tutorials/videos, I see that the conda/virtual environments are recognized (seemingly natively). And yet I cannot seem to figure it out.
With the python script I want to run open in VScode, I ensure that I am using the correct interpreter/environment as follows:
ctrl+shift+p to load Command Pallette
Python:Select Interpreter
C:...\Anaconda3\envs\p3audio\python.exe
At the bottom of the window, the environment also appears to be pointing to the environment as I loaded the p3audio environment to use python 3.7 and this is what is showing ('Anaconda Python 3.7.0') (The top Python in top level anaconda is shipped with 3.6 I believe)
When I try to run the python file by 'Run Active File' from the Terminal Menu, it automatically loads powershell and executes this into the terminal:
PS C:...\Anaconda3\envs> c:...\Anaconda3\envs\Projects\p3audo_projects\my_script.py
(the second path is in yellow)
If I try to activate the conda environment manually from the powershell terminal, it doesn't recognize it.
I can run the script fine from the anaconda prompt terminal.
I think it may be a path issue, but all of the documentation/resources I used suggest I shouldn't have the issue if I didn't select (tick the box) for add path on install. I checked my environment variable path and don't have anaconda on it.
I am wondering if it has to do with the 'you can change the default terminal shell by selecting the customize button' that loads in the terminal window. This leads me to believe that I have to manually add the conda terminal path somewhere. But this surprises me because, vscode shipped with anaconda, and I see no mention of this in the documentation.
I am obviously a newbie at this, and have suffered through environment variable/path issues many times. But I can't figure this out.
Any help would be greatly appreciated.
Thanks
For anyone struggling with this, I'll tell you what worked for me. This was after four hours of trying to get Python to run in PyCharm and VS Code.
Basically dldatacowboy has the answer, but the argument for terminal.integrated.shellArgs.windows needs to be reformatted (maybe only if you're on a Windows machine?). There should be no space before the "K", and the backspaces need to be double backspaces. So my settings in my settings.json file look like so:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/K","C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat C:\\ProgramData\\Anaconda3"]
Make sure to change the path to whatever comes up when you follow dldatacowboy, but then change the backslashes to double backslashes.
For people working in Mac OS.
In order to setup the integrated terminal in VS Code to use the active Anaconda Environment's Python by default, you will need to modify your User or Workspace Setting's 'settings.json' with the following line:
"terminal.integrated.shellArgs.osx": [],
For reference: this closed issue addressed the topic.
Ok I figured it out. VSCode is awesome.
Go to Settings (ctrl+,)
Decide if you want this custom terminal to be applied at the User Settings or Workspace Settings, and select that (located just below the settings search bar)
Open settings.json (don't know shortcut key yet, it is the three-dot menu located to the far right on the same row as the User Settings and Workspace Settings tabs.
Locate "terminal.integrated.shell.windows" in the Default User Settings (left panel), again you can enter it into the search bar to locate it.
Copy it over to the User Settings/Workspace Settings (depending on what you selected or now decide to select) (in the right panel)
e.g. copy:
"terminal.integrated.shell.windows": "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe",into json array on the right
Edit the path to equal your cmd executable path
e.g. "terminal.integrated.shell.windows":
"C:\Windows\System32\cmd.exe",
You need to create the argument, which is what I was confused about:
Go to start menu in windows, locate the Anaconda Prompt Open File Location with right click, inspect properties and see Target Location Path. Don't worry about the first string, copy from after the cmd.exe to the end. Use this for the "terminal.integrated.shellArgs.windows"
e.g. "terminal.integrated.shellArgs.windows": [
"/K", "C:\Users\User-1\Anaconda3\Scripts\activate.bat C:\Users\User-1\Anaconda3"
Make sure you separate with a comma and properly contain strings with all four quotations. Save Close vscode. load it up and you are good to go.
Hope this helps someone! = )
With the recent conda release (4.6.1) Conda can now be initialized in Powershell and can now be integrated in VSCode cleanly. I made a stackoverflow post here
Please use the following settings in vs code and it ought to work, provided your conda is installed appropriately. Also, please make modifications according to your environment.
settings for powershell is mentioned below
{
"workbench.startupEditor": "newUntitledFile",
"editor.wordWrap": "on",
"editor.wordWrapColumn": 100,
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"terminal.integrated.shellArgs.windows": [
"-ExecutionPolicy", "ByPass", "-NoExit", "-Command", "&",
"'C:\\Users\\kpache\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\kpache\\Anaconda3'"
// "/K", "C:\\Users\\kpache\\Anaconda3\\Scripts\\activate.bat", "C:\\Users\\kpache\\Anaconda3"
],
"terminal.external.windowsExec": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"workbench.colorTheme": "Default Light+"
}
It looks like you've been mislead by the title of this article.
If you read the article, it says Anaconda comes with Visual Studio Code included. So it would be easier to setup if you download and install the Anaconda setup.
If you look at the article above, it shows in one of the installation steps, the option to install VS Code.
The following is the download link:
Sharing what worked for me on MacOS.
For latest versions of Anaconda (v 2019.10 at the time of this writing) add this line to your .bash_profile:
export PATH="/opt/anaconda3/bin:$PATH"
And for older versions it may look like:
export PATH="/anaconda3/bin:$PATH"
Hope that helps someone.

How to tell VScode, on my Mac running high Sierra, which version of python to use

I use VScode to write and run python. I use it with a Jupyter extension. I have 2 virtual environments created using Anaconda. One py27, for python 2.7.13 and the other, py36, for Python 3.6.5.
I have a simple code which I edited to only run in python2. When I run VScode, the virtual environment I run it in does not seem to matter. VScode seems to run but I can't figure out how to make it use py36 (python 3.6.5) or py27 (python 2.7.13).
By using different commands in the command palette, or double-clicking on phrases in the bottom border of VScode I can eventually get it to run which version I want. But I have not found a reproducible method.
I have tried about 100 times with no luck. I thought I found a method that would repeat but I tried it again and it didn't work.
I know this question is very vague but it would take pages to explain what I've tried. I have about 5 or 6 settings which I've tried a plethora of combinations. One of the commands I tried is to select a python interpreter. I have several options but none seem to have an effect. Also, in the bottom border of the VScode window, there is the name of a specific python interpreter. You can click on this to select a different python interpreter. As far as I can tell this doesn't do anything. Actually, it's a hindrance because a user may think they are actually using that interpreter.
I've tried both py27 and p36 but I get no difference. If I can get visual studio code to start out in py27, I can make it change to py36 and back to py27. But how to make it start in py27 seems random. My virtual environments are in my home directory, /User/myname/py27 for example along with the VScode app.
The documentation at https://code.visualstudio.com/docs/python/environments explains how to select your virtual environment. Without knowing where you installed the virtual environments the best I can tell you is you can manually specify what interpreter to use with the python.pythonPath setting or if you have both virtual environments in a directory outside of your workspace folder you can specify the common folder with the python.venvPath setting.

How can I access different Anaconda environment from Pycharm (on Windows 10)

I have installed anaconda with python 3.5, and created a new environment with Python 2.7 (on windows 10).
I can easily change the Anaconda environment with the command line tool. However in Pycharm, when I try to change the Python interpreter, I can only see the Anaconda Python 3.5 version.
Is there a easy way to select the Anaconda environment from Pycharm?
In PyCharm you can add how many interpreter you want. You have to go in
Settings->Project->Project Interpreter
Here, you should see the current interpreter.
At the top of the window, there is a list which contains all local interpreters.
If you find it in the list select it, otherwise, in the right corner of the window, there is a settings icon; press it and press on Add local.
After this navigate in your computer and select the directory that contains the Anaconda env.
edit
As PatriceG suggested in comments, if you already have an Anaconda environment but you don't know where is it or you don't remember, you can check the current position using the following command:
conda info --envs
Then, you should be able to find and select it.
Now, you have to add the anconda env to your local interpreter, so you are able to use it.
Go to Setting then Project Interpreter Click on Setting icon which will lead to browse folder, Choose Add Local then on left side of page, click on system Interpreter then Press OK and Apply.

Categories

Resources