DEV Community

Cover image for Python Tools for Managing Virtual Environments

Python Tools for Managing Virtual Environments

Jonathan Bowman on August 23, 2020

A Python virtual environment is "a self-contained directory tree that contains a Python installation for a particular version of Python, plus a num...
Collapse
 
lico profile image
SeongKuk Han

Thank you so much!

Collapse
 
yarongon profile image
yarongon

Great read!
I'm currently using miniconda for managing both Python versions and virtual environments. I'd like to move to Poetry for managing virtual envs, however, as far as I understand, Poetry's virtual envs are coupled with the directory. What I like about miniconda is that the virtual envs are decoupled from the directory. You simply conda activate it.
Do know if Poetry can work like that?

Collapse
 
bowmanjd profile image
Jonathan Bowman • Edited

That is my understanding as well: Poetry is for project management. A project lives in a directory. And the project gets its own virtual environment.

In other words, they are solving slightly different problems.

If you are primarily interested in managing environments with different Python versions, you may be interested in coupling Poetry with pyenv, and utilizing the poetry env command.

You can read a fuller explanation in the Poetry docs.

Collapse
 
plastikfan profile image
Plastikfan

What do you mean by project management? Do you mean building the package, linting, running tests, etc, as opposed to managing the environment? This needs to be explicit because the difference between poetry and say conda is very subtle and I am trying to really understand the difference between the 2. Since poetry can manage a project's environment, does that mean we can have a conda env at the same time as a poetry project env? In this scenario, the developer would install a projects deps into the poetry env, and then install wider dependencies into the conda env. Does that make sense or is it total rubbish!?

Collapse
 
jefftriplett profile image
Jeff Triplett (he/him)

Nice article.

I'm a fan of pyenv and virtualenv, but I install pyenv + pyenv-virtualenvwrapper and I skip pyenv-virtualenv which gives the nice features of virtualenvwrapper without needing the rest. So workon, mkvirtualenv, and hooks all work well.

Throwing this out there for anyone wondering if virtualenvwrapper still works.

Collapse
 
bowmanjd profile image
Jonathan Bowman

Really good point! Thanks for pointing that out.

I didn't explore virtualenvwrapper in this article because I was hoping to be cross-platform, and I don't believe virtualenvwrapper works on Windows. I would love to be wrong, as I hear good things about it.

Collapse
 
99hats profile image
99hats

So how do you install a requirement to hatch??

Collapse
 
bowmanjd profile image
Jonathan Bowman

Are you just curious, like me, or are you considering using hatch? Did hatch install [package] work for you?

I assume the responsibility is on the developer then, to do a pip freeze > requirements.txt or the like. I'd love to hear differently if you figure it out, though.

Collapse
 
99hats profile image
99hats

Hatch uses pip, so hatch install [package] works. The docs could use a little more rounding out.

After using pipenv, poetry, pyenv-virtualenv, hatch, flit and pyflow, I've landed on hatch for windows as the most tolerable. I think I've run into trouble trying to use pyenv-win which isn't necessary. Instead one can just install any python version and run it as py -3.7 or py -3.8, so no need to mess with your paths.

On windows when using hatch shell I lose readline which I can't live without so I just use venv\Script\activate.

Thank you for your enjoyable articles. A good read even if I think I know the subject.

Thread Thread
 
99hats profile image
99hats

And adding to requirements is manual. Usually I add it to the configurable part of setup.py. I don't think this is in the docs anywhere either but there is a note in setup.py.

Thread Thread
 
bowmanjd profile image
Jonathan Bowman

Fascinating. Glad you are finding hatch useful. I should dig into hatch more.

I totally agree that pyenv or pyenv-win is often unnecessary when on a system (such as Windows or Fedora Linux) that allows the installation of multiple Python versions easily.

Readline. So necessary. I use ptpython a lot on Windows, as well.

Thank you! I am honored that you are reading.

Collapse
 
marcusobrien profile image
Marcus O'Brien

I use Conda for ML, due to the fact its easy to set up the right versions of all the python packages that work with CUDA, so I can use my GPU. I also use WinPy with venv to create simple python environments. I try to never use any system installed python, so on my Windows path I have no python, pip, python3, pip3 etc. This prevents a colossal amount of confusion I had when starting out with Conda and virtual environments. Now I know I am installing with the right pip for example because there isn't any other on my path, the only one is the venv or conda pip and python after I activate. So, I run a WinPy shell to create any new venvs, or conda shell for conda envs, then I can activate these from any shell when I want to use them.

But my question is, is there an easy way to manage all these envs, something that lists all the versions, creates versions in the same parent folder, where you can run a shell and activate one etc. I find myself forgetting where I have installed everything, so I have a listpyenvs.bat that lists all my venvs as they are in the same root folder.

I would like a small GUI app that I can click on to start a shell - currently I use Windows Terminal and have it setup to start a cmd shell and activate the pyenv etc, but I have to keep adding new pyenvs to the Windows terminal json config file. So is there anything that manages all these environments ?

Collapse
 
plastikfan profile image
Plastikfan

Great article. I'm just starting out with Python and drowning in a sea of complexity. The language is easy to learn, but the myriad of tools isnt. I'm a dev, looking to create new packages and clis, not a data scientist/or ai specialist. I am using cookietemple to boilerplate new projects and using poetry & conda together. I havent yet found a good article/documentation that explains using both together. I understand what an env is, and how to manage with conda, but an issue I have is package dependencies. If I decide that in creating my-package, I need a dependency (not a dev dep), should I install that with conda or poetry? I think that it should be poetry because as I understand it, if I install with conda, that installs to the environment, when I really want it to be a package dependency. So am I right with this?

Collapse
 
cryptogeekus profile image
Crypto Geek

I have never used virtual Envs. I will start using them. Thank you so much for this share.

~Bhaskar

Collapse
 
soapergem profile image
Gordon Myers

Or suggest a tool I missed in the comments!

Would you consider updating this guide to cover pdm? They claim to do all the things Poetry does, but better.

Collapse
 
bowmanjd profile image
Jonathan Bowman

Thank you for that suggestion, @soapergem ! When I next update the article, I will certainly consider pdm.