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

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.

Related

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

Windows: How to configure multiple versions of Python to work together?

I installed Python 2.7 a long time ago on my PC (I am running Windows 10). Today I decided to install Python 3.7, but after typing 'python' into the command prompt the console printed 'python 2.7...'
I have no idea as to what I should do. Would this be a problem with my path configuration? I considered uninstalling python 2.7 but I still want it installed on my computer.
Configure multiple Python versions on Windows
Python 3.3 introduced the Python Launcher for Windows. Rather than using python.exe, call py and select the version with flags:
py -2.7
py -3
System-wide installations of Python 3.3 and later will put the launcher on your PATH.
Note on Virtual Environments (new in Python 3.5)
If the launcher is run with no explicit Python version specification, and a virtual environment (created with the standard library venv module or the external virtualenv tool) active, the launcher will run the virtual environment’s interpreter rather than the global one. To run the global interpreter, either deactivate the virtual environment, or explicitly specify the global Python version.
Outdated Method (not recommended)
Rename executables - After installing multiple versions, change the names of the executables. For example, for Python 2.7, under the installation folder, rename python.exe to python27.exe and rename Python 3.7 from python.exe to python37.exe. Then on the command line, select the version by entering python27 or python37. Whichever version is preferred, could be left as just python.
Add Path Environment Variables - For example, on Windows 10, go to the Windows menu and search for "environment variables" and click edit the system environment variables. In the System Properties dialog, click Environment Variables.... Under "System variables", select "Path". Click Edit.... Click New and add the first entry below. Click New again for each Path variable entry.
C:\Python27
C:\Python27\Scripts
C:\Python37
C:\Python37\Scripts
This will enable Python and pip. Be sure that paths match your actual installation directories.
I would suggest using pyenv
I have been using it and is working well for me. Some of the handy features of pyenv are
It allows installing multiple version on python easily
It allows switching python version with one command in global, shell or folder level
It also allows creating virtual env using virtualevn extension
I would suggest to use virtual environment. Soon or later you would you might get dependency problems.
You Can Configure The Python2 & python3 In Windows PC Like This:
First Of All Install Python 2 & Python 3 in windows PC in C directory like this:
C:\Python27 --> For Python2
C:\Python39 --> For Python3
After Installing Both Packages Go To Their Respective Folders And Copy python.exe & Paste In The Same Directory and rename the python - Copy with python2(In python27 folder) & python3(In python39 folder)
Python2:
Python3:
And Then Set Environment Vairable Like This:
All Done Now You Can Run Any Script Which May Compatible With Python2 or Python3 :)

Activating conda environment without separate Python version in IDEs

I recently created a conda environment (in Ubuntu 16.04), which is working great from the command line. I can activate it with no problems, the packages are properly separated, etc.
However, because it is running the same version of Python as the Anaconda installation (Python 3.6.1), it has not created a separate interpreter. Within the /anaconda3/envs folder there are no other folders. This makes it difficult when I want to use editors (such as vscode and Pycharm) because they cannot find the path to some of the packages I have installed in the environment. I would love to start using the great debugging features in Pycharm but without a separate interpreter I have no idea where to even start. Is there a way to initialize the interpreter in the correct environment?
When I run conda info --envs, this is what I get:
# conda environments:
#
chatbot /home/bradley/.conda/envs/chatbot
tensorflow /home/bradley/.conda/envs/tensorflow
tf_testing /home/bradley/.conda/envs/tf_testing
root * /home/bradley/anaconda3
If I try to run the Python in these directories (a small executable script exists), it doesn't include all the packages. Should I maybe add an environment variable? If so, what would I write?

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:

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