How to keep track of which pyenv virtualenv goes with my project?

590 Views Asked by At

pyenv virtualenv puts virtual environments somewhere besides the directory they were called in. This means that when I come back to a project months later, I'm left to guess at what virtual environment I had set up for it.

Being a trivial problem, I suspect there is a convention in the community. Is this the case.

How does one keep the association between a project folder and the pyenv virtual environment it uses?

1

There are 1 best solutions below

1
cherry On

Here are steps that cover the basics nicely.

Extending on Pavel's answer this is how you create venv in python:

python -m venv myproject-venv

This will create a folder called myproject-venv in your current directory.

To answer your question, we can create this environment folder (Post venv command) anywhere on the system but for simplicity, this is created under the root folder of the project.

Post that we need to make sure that we include myproject-venv in .gitignore folder which will prevent it from committing the entire folder to the version-controlling system.

What usually goes into the source control is the requirements.txt file which includes all the dependency/packages you installed in virtual env mode. Think of this as an isolated mode where python is freshly being installed with no prior knowledge of any package.

Nicely written thread about requirements.txt: Automatically create requirements.txt

Useful commands in venv (on Linux system)

Activate venv: source myproject-venv/bin/activate

Exit from venv: deactivate (This command is available only when you are in venv)

Hope this helps. Thanks.