VSCode does not automatically activate python interpreter of conda environment in WSL2 - python

I have installed (Mini) Conda in an Ubuntu 20.04 instance running on WSL2, and I have created a virtual environment (myenv).
First, the path to the interpreter does not appear when I perform Python: Select Interpreter, so I have manually inserted it.
However, upon opening a new instance of the terminal, source activate myenv is not automatically run. I think VSCode is not aware that I am working with WSL2 (e.g. when opening the dialog box for selecting the interpreter, it searches only for windows executables).
Has someone encountered the problem and solved it?

Related

Conda environment not showing up in VS Code

I installed miniconda on Windows 10 and created an environment (I followed this guide: https://www.notion.so/shashankkalanithi/Setting-Up-Conda-Environment-ba83f7f019ea44b9af37588eed419eb9). However when I open the VS Code I don't see that environment python interpeter on kernel list. There is only one interpreter on the list: ~\Miniconda3\python.exe
How can I fix this?
in vscode press ctrl+shift+p and type python:Select Interpreter you should see all the environment there. If it does not appear create a .py file and try again. also you can press the reload icon on the search bar where you typed python:select interpreter.
You can try to follow methods from
vscode - Create a conda environment
Additional notes:
...To ensure the environment is set up well from a shell perspective, one
option is to use an Anaconda prompt with the activated environment to
launch VS Code using the code . command. At that point you just need
to select the interpreter using the Command Palette or by clicking on
the status bar.
Firstly you need to create an environment with python in it otherwise it won't recognize it. Create an environment like this first
conda create --name tf26 python==3.10
Use your preferred name and python version here thereafter restart VS Code. You will definitely see your env.
Note:
You can delete any unused env if want like this
conda env remove --name <env_name>
I wanted to use the new environment as a Jupyter kernel and had to install the jupyter package for it to show up in the kernel selection of VSCode. You can install it by running conda install jupyter.
I finally resolved the problem.
This thread says that you need to create the conda environment passing the python argument:
conda create -n your_env_name python=3.7
Doing this the environment appears in the Select interpreter to start Jupyter server options.
The extension automatically looks for interpreters in the following locations:
Conda environments that contain a Python interpreter. VS Code does not
show conda environments that don't contain an interpreter.
After you create a conda environment, you need to activate it and install some packages in order to get the python interpreter. And remember to reload the VSCode. If it still does not exist, you can try to choose Enter interpreter path, to point the path manually.
In your project .vscode/settings.json file, just replace the old python.pythonPath setting with the new one (or add if non-existing) python.defaultInterpreterPath and it will work. The value for the setting is the path to the venv you're using in your project.
Afterward, Ctrl+Shift+P via Python: Select Interpreter will allow you to choose a different interpreter.
I had the same problem.
After I opened Anaconda Prompt as Administrator and created the environment, I saw it in VS code
Open “Anaconda Prompt” from the Windows start button as
“Administrator.”
The reason why vscode doesn't show the environment is that it doesn't have a python interpreter in it because of inheriting or something.
The solution is also simple. Just manually install python in that environment.
conda install python

Python 2.7 Script in Virtual Environment

I am running a Python 2.7 script that has specific dependencies/libraries (contained in a virtual environment) using anaconda prompt. Is there a way to run the script using code in a python 3 .py file in a different environment? Something like a library that allows me to open anaconda prompt in a specific environment (to then run the python 2.7 script). I couldn't seem to find it online. Any pointers would be appreciated.
Well you can select the environment from the drop-down box in the Anaconda Navigator home page if you have your virtual environment in Anaconda itself.
Or you can use
conda activate env-name
If you have your base conda in your terminal as default or else activate using
source ~/.bash_profile
then run
conda activate env-name
Hope this helps.

WSL Conda Environment in PyCharm

I'm trying to set up a new dev environment on Windows 10. Let me set the stage with some key points:
I am using Ubuntu through Windows Subsystem for Linux (WSL).
I have Anaconda3 installed on WSL.
I have PyCharm installed on Windows 10.
I am attempting to use a Conda environment created in the WSL within PyCharm on Windows 10. For some reason I cannot find documentation or guidance on the process.
I have already successfully setup a virtualenv within WSL as a remote interpreter for PyCharm however it will not allow me to do the same with a Conda environment.
This was possible by using the absolute path for the conda interpreter from within PyCharm.
e.g.
C:\Users\winuser\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\home\wslusr\anaconda3\envs\myenv\bin
It is now (as of very recently) not.
After some trial and error along with contacting JetBrains support, it was determined currently not possible.
My solution to get around this was to have Anaconda installed on both Windows and WSL and then create the Conda environment within Windows. I was then able to activate this environment within WSL with the absolute path to the environment.
Not the best approach but allows for editing in WSL and usage of the interpreter in PyCharm.
In your project in Pycharm.
Choose File, Setting, Project, Python Interpreter, Add
Choose WSL on the left. Linux = your Ubuntu
Python interpreter path = home/<your_name>/miniconda3/envs/<your_env>/bin/python3 -- this is the environment you have created in Ubuntu with Conda.
I haven't been able to confirm that yet, but for me it seems that the direct integration of WSL in PyCharm as Interpreter (like describede here: https://www.jetbrains.com/help/pycharm/using-wsl-as-a-remote-interpreter.html) is only possible when using the Professional Version of PyCharm and not the Community Edition.
There is a way you can use all the conda environments in wsl as well.
Install Anaconda or Miniconda in your wsl
Set the CONDA_ENVS_PATH environment variable in your wsl to where your conda environments reside.
(By default it will be /Users/.../anaconda3/envs/)
That's it after you restart your terminal and type conda list env. You can see all your environments in windows.
Incase if you dont know how to custom path for your cond envs:
check custom

Using Conda Environment from PyCharm

I've looked at all the responses in the search and specifically Use Conda environment in pycharm without resolving my problem.
I'm on Win10 using the latest Anaconda and PyCharm for Python 3.6. The situation is that I've created a new environment using Conda which uses the Python version available in the base environment. Therefore, there is no python.exe in the newly created environment. In PyCharm, when I try to select the Conda environment, the dialog apparently does not see a Python there and refuses to set that as the environment.
In PyCharm, I create a new Project (pure Python) and set the location for the project in my PyCharmProjects directory. I then try to select existing interpreter and navigate to the appropriate Conda environment which I'm unable to select.
I'm not sure if there is something I don't understand or there is actually a problem here, but I'm getting a bit frustrated trying
to find the proper approach. Any help appreciated.
--Don
When you create the conda environment from cmd prompt do you explicitly tell it what python you want to use?
conda create --environmentName python=3.6
That should place a python.exe in your environment.

How to manage (ana)conda with pycharm 2016.3 in linux

I would like to use Anaconda and the newest Pycharm 2016.3 together. I would like to be able to manage packages in settings->interpreter. If this is not supported, I would like to know the workflow of using these two together. According to another SO question, Pycharm 5 used to have a 'Create conda env' in the interpreter settings, but this seems to be gone now. I have tried:
1) Manually creating a virtual environment with 'conda create --name project numpy' and I add the interpreter ('~/anaconda2/envs/bin/python', the location of python for my created virtual environment. However, pycharm doesn't allow me to add any packages through settings->interpreter. Running an 'import numpy' through the console shows errors that are pointing to /usr/bin/python, not my virtual env python, and an error 'ImportError: cannot import name multiarray'. I'm not sure what package to add using conda from the cli, and the pycharm frontend doesn't add packages
2) I've tried the same as 1) but with my global anaconda python as the interpeter ('~/anaconda2/bin/python') and it doesn't seem to be able to connect to the console.
3) Creating a virtual environment through pycharm directly.
I would like to remove my default pythons (/usr/bin/python2.7/3.5 from the list of interpreters in pycharm) for debugging purposes but it won't let me and it seems to be showing packages that my anaconda virtual env doens't have installed.
Is there a way to manage my VIRTUAL enviornment in Conda using pycharm? If not, what steps do I take to make these two play well together assuming I can't manage it through pycharm interepreters settings.
This is the normal steps that i follow when i use virutalenv with PyCharm
I normally work on ubuntu
First, i always create a separate environment for every project using the command virtualenv "environment_name" from the command line.
Activate the environment using the command - source environment_name/bin/activate in ubuntu.
Suppose if i want to start a django project, i create the project using the command django-admin startproject project_name
Open this project in pycharm.
go to settings-> interpreter in pycharm. choose "add local" interpreter from the settings. It will open a pop-up. Go to the directory of the environment you just created and select the correct python interpreter you want to use.
now if you want to install a new package, you can go to interpreter settings and add package from the pycharm or you can fire up the command line, activate the environment and run pip install package_name. Once the package is installed, it will also show in pycharm.
if you are using Windows OS, use powershell to execute the above commands. The only difference will be in activating the environment. In windows, to activate an env use environment_name/Scripts/activate
EDIT:
Same goes anaconda environments also, the easy way is to manage the environment from the terminal and pycharm will show the packages changes in the interpreter settings.
Here is what I have been doing and it works great. I create a virtual environment separately and when I create a new project in PyCharm, I point the interpreter to the python from the virtual environment. An example is shown in the picture below:

Categories

Resources