WSL Conda Environment in PyCharm - python

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

Related

OS X "which python" points to python2.7 instead of Anaconda python3.9

I factory reset my MacBook and set up everything new to get rid of the multiple python version chaos I had on my old laptop.
Only thing I have installed atm is Anaconda. I created a new conda environment with python 3.9 and activated it in my zsh Terminal.
Still when I type "which python" it shows "/usr/bin/python" containing the MacBook standard Python2.7 version.
How can I make environments use their own dedicated version?
Edit for Code:
conda activate myenvname
which python
Trying to access the folder where the anaconda python should be leads to
cd: no such file or directory: /Users/marcus/anaconda/bin
When you install anaconda through the terminal it provides an option to prepend anaconda to the system path. make sure to choose yes. If you don't, you'll need to provide the full path to your anaconda installation when activating an environment, as in:
source <path to conda>/bin/activate
conda init
You'll need to do this in each shell session. Alternatively, you can use the anaconda terminal.
To fix this, Anaconda does not recommend fixing your path manually. I think this is because there are a number of things anaconda does when setting up your shell if you choose "yes". Instead, the recommended fix is to uninstall and re-install anaconda (and pick yes this time ;)).
See the Anaconda docs for more information.

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

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?

How to Configure Poetry Environments in Pycharm With Windows + WSL2?

TL;DR: can't configure a Python Interpreter on PyCharm (Windows) using an existing Poetry environment in WSL. When trying to set the Poetry environment path under Add Python Interpreter > Poetry Environment > Existing Environment, the needed Python executable simply does not show. What am I doing wrong?
====================================================
Details:
I'm using PyCharm Pro 2021.3 on Windows 11, with Python running on WSL2 (Ubuntu 20.04). I am trying to add a python interpreter for an existing Poetry environment I created on WSL2, and it just does not seem to work.
PyCharm's current support of Poetry is via adopting this plugin. From what I could gather from the plugin's official documentation, in order to define an interpreter with an existing Poetry environment, I go to Python Interpreter > Add > Poetry Environment, choose Existing environment, and put in the path to that specific environment:
In order to find the path to that environment, I run "poetry env info", which gives a path in the following pattern:
\\wsl$\Ubuntu-20.04\home\$USER$\.cache\pypoetry\virtualenvs\my-pretty-project-<some-hash>-py3.8\
When running which python in the environment, I see the python executable is at:
\\wsl$\Ubuntu-20.04\home\$USER$\.cache\pypoetry\virtualenvs\my-pretty-project-<some-hash>-py3.8\bin\python
However - when I browse to that location in PyCharm, the Python file simple does not show. The bin directory appears as empty - as also described in this question. However - and similarly to what described in said question - if I try to redefine the default interpreter path for WSL to be the path to that Poetry environment, the Python executable is there and kicking:
(The solution described in the aforementioned question, sadly, does not work for my problem, as I am already using the patch to the Poetry environment).
What can I do to make this work?
Let me get this straight: You want PyCharm for Windows to execute Python binaries in WSL?
That cannot happen.
Binaries in WSL are "ELF" binaries which Windows cannot execute (outside WSL). If the virtualenv was created by poetry from within WSL, it will contain ELF Python binaries. And that is why PyCharm for Windows won't ever pick it up. Because ultimately PyCharm for Windows relies on Windows to execute the binaries, as long as you don't choose the WSL option.
Explicitly selecting the "WSL" option indicates to PyCharm that for this particular virtualenv you want PyCharm to invoke the binaries using WSL.
The solution is either to re-create the virtualenv in Windows, or just use the "WSL" option but you have to manage the poetry manually via WSL shell.

Can I use my Python or Conda environment of my Windows 10 on my Windows Subsystem Linux (WSL)?

I installed and set up all my necessary lib and environment in my Windows OS.
I just install WSL and trying to code on WSL, and I wanna know whether I can reuse my available Windows's Python or Conda environment on WSL. If I can, how can I do that?
Thank you very much!

Using (Ana)conda within PyCharm

I've got Pycharm 4 running on my Linux (Ubuntu 14.04) machine. In addition to the system python, I've also got Anaconda installed. Getting the two to play nicely together seems to be a bit of a problem... PyCharm provides some interesting integration for virtualenvs and pip, but the Anaconda Python distribution seems to prefer using its own conda tool for both activities.
Is there a relatively simple/painless way to be able to use conda in conjunction with PyCharm? Not just as an alternative interpreter i.e. point PyCharm at the Anaconda Python binary for a project interpreter, but to be able to create, source/activate and deactivate virtual envs, add/remove packages in those virtual envs, etc.
Or am I going to have to choose between using Anaconda (and having a more recent and up-to-date python than may come with the system), and being able to use PyCharm's features to their fullest extent?
I know it's late, but I thought it would be nice to clarify things: PyCharm and Conda and pip work well together.
The short answer
Just manage Conda from the command line. PyCharm will automatically notice changes once they happen, just like it does with pip.
The long answer
Create a new Conda environment:
conda create --name foo pandas bokeh
This environment lives under conda_root/envs/foo. Your python interpreter is conda_root/envs/foo/bin/pythonX.X and your all your site-packages are in conda_root/envs/foo/lib/pythonX.X/site-packages. This is same directory structure as in a pip virtual environement. PyCharm sees no difference.
Now to activate your new environment from PyCharm go to file > settings > project > interpreter, select Add local in the project interpreter field (the little gear wheel) and hunt down your python interpreter. Congratulations! You now have a Conda environment with pandas and bokeh!
Now install more packages:
conda install scikit-learn
OK... go back to your interpreter in settings. Magically, PyCharm now sees scikit-learn!
And the reverse is also true, i.e. when you pip install another package in PyCharm, Conda will automatically notice. Say you've installed requests. Now list the Conda packages in your current environment:
conda list
The list now includes requests and Conda has correctly detected (3rd column) that it was installed with pip.
Conclusion
This is definitely good news for people like myself who are trying to get away from the pip/virtualenv installation problems when packages are not pure python.
NB: I run PyCharm pro edition 4.5.3 on Linux. For Windows users, replace in command line with in the GUI (and forward slashes with backslashes). There's no reason it shouldn't work for you too.
EDIT: PyCharm5 is out with Conda support! In the community edition too.
Change the project interpreter to ~/anaconda2/python/bin by going to File -> Settings -> Project -> Project Interpreter. Also update the run configuration to use the project default Python interpreter via Run -> Edit Configurations. This makes PyCharm use Anaconda instead of the default Python interpreter under usr/bin/python27.
as per #cyberbikepunk answer pycharm supports Anaconda since pycharm5!
Have a look how easy is to add an environment:
Continuum Analytics now provides instructions on how to setup Anaconda with various IDEs including Pycharm here. However, with Pycharm 5.0.1 running on Unbuntu 15.10 Project Interpreter settings were found via the File | Settings and then under the Project branch of the treeview on the Settings dialog.
this might be repetitive.
I was trying to use pycharm to run flask - had anaconda 3, pycharm 2019.1.1 and windows 10. Created a new conda environment - it threw errors.
Followed these steps -
Used the cmd to install python and flask after creating environment as suggested above.
Followed this answer.
As suggested above, went to Run -> Edit Configurations and changed the environment there as well as in (2).
Obviously kept the correct python interpreter (the one in the environment) everywhere.

Categories

Resources