-3

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?

sinoroc
  • 18,409
  • 2
  • 39
  • 70
pixelearth
  • 13,674
  • 10
  • 62
  • 110
  • 1
    How is that any different to what `poetry` or `conda` do? The only common virtual environment manager that places the venv in the project folder is `venv` – NotAName Mar 29 '23 at 00:31
  • I typically use tox or nox which place virtual environments in a `.tox/` or `.nox/` directory in the project respectively. – flakes Mar 29 '23 at 00:36
  • Your `.python-version` file can also hold the name of a virtualenv, so if you have it set up, just being in that folder will use that environment by default. – BeRT2me Mar 29 '23 at 00:48
  • you can give custom name to virtualenv like `venv_` something like that or in your project keep a `venv` folder and add that in `.gitignore` for more better handling – sahasrara62 Mar 29 '23 at 01:11
  • @BeRT2me if that works that solves all the problems. If you write up a functioning example in an answer I'll accept it. – pixelearth Mar 29 '23 at 03:14
  • @pavel my day job isn't with python. I have no idea what `poetry` or `conda` do. I like python, I really do, but for the last 10 years every time I try to do something in python there's some reason for it to be a complete sh*# show and I spend most of my time on the environment setup, being lost, and finally giving up. It used to be the whole python2/3 thing, but that seems to have mostly passed. Now the whole virtualenv situation is a morass of options and confusion. But either way your comment doesn't answer the question of how to connect the project to which virtual env it needs. – pixelearth Mar 29 '23 at 03:20
  • My comment didn't answer your question because the question was not phrased correctly. If you simply want to create a virtual environment in your working directory then you can simply do `python -m venv .venv`. This will create a virtual environment in a folder `.venv/` in your project root directory. Then you call `.venv/Scripts/activate.ps1` on Windows or `source .venv/Scripts/activate` on Linux to activate the new environment and you can start installing packages using pip – NotAName Mar 29 '23 at 03:58
  • 2
    Virtual environments do not inherently "go with" projects, no matter where they are put. Nothing stops you from activating any virtual environment on your computer and then running any Python code on your computer with that virtual environment - of course, it might not work, if that particular venv doesn't have the right dependencies installed etc. That said, if you want to understand exactly where "somewhere besidest the directory" is, why not start by **reading the documentation**? – Karl Knechtel Mar 29 '23 at 05:05
  • 2
    The post and comments here overall read more like a rant than a sincere attempt to ask a question. I attempted to fix that, but there are several things that it seems like you might be asking, and I'm pretty sure you intend to ask more than one of them. That's not how Stack Overflow works - we want clear, specific questions that are **one** question at a time, so as to help build a searchable Q&A library. We don't provide answers to try to help individuals with general understanding of a topic; for this, please try a discussion forum, which Stack Overflow is **not**. – Karl Knechtel Mar 29 '23 at 05:06
  • "_How does one keep the association between a project folder and the `pyenv` virtual environment it uses?_" Let's be careful with the wording here to avoid confusions, `pyenv` itself does not deal with Python virtual environments. You seem to be using a specific `pyenv` plugin. And I guess this plugin has its own opinionated way of doing things. If this plugin does not satisfy your expectations, you can opt to not use it. – sinoroc Apr 05 '23 at 13:52
  • "_I suspect there is a convention in the community._" The habit I follow (and as far as I can tell many others do as well) is to have the virtual environment directly within the project's root under the `.venv/` directory. Some have tried to make this habit into something more of an official conventions, but it has its pros and cons (just like anything else). For example [PEP-704](https://peps.python.org/pep-0704/) which is now withdrawn. See also this [discussion](https://discuss.python.org/t/setting-up-some-guidelines-around-discovering-finding-naming-virtual-environments/22922). – sinoroc Apr 05 '23 at 13:57
  • You can get a list of virtual environments created by `pyenv virtualenv` (the plugin), by running the command [`pyenv virtualenvs`](https://github.com/pyenv/pyenv-virtualenv#list-existing-virtualenvs) (plural), and this also tells from which directory it was created (so it might help identify the project the environment is meant for). Apparently the plugin can also help with auto-activating the environment when `cd`-ing into the directory, so the association is automatic and nothing needing to think of ([doc](https://github.com/pyenv/pyenv-virtualenv#activate-virtualenv)). – sinoroc Apr 05 '23 at 14:02
  • @sinoroc is there a way for `pyenv` to make the virtual env in the directory? That would be straightforward. But I'm not sure there is. – pixelearth Apr 05 '23 at 17:01
  • @sinoroc I think you may be right I may be confusing `pyenv` with `pyenv`. I wasn't aware there was more than one thing called `pyenv`. – pixelearth Apr 05 '23 at 17:02
  • I'm using `brew install pyenv`, which handles virtual environments as well as python versions – pixelearth Apr 05 '23 at 17:04
  • `pyenv` can not create virtual environments. You seem to have installed an be using the `pyenv-virtualenv` plugin. I recommend you read its [documentation](https://github.com/pyenv/pyenv-virtualenv#using-pyenv-virtualenv-with-pyenv). – sinoroc Apr 05 '23 at 17:06

1 Answers1

1

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.

cherry
  • 24
  • 4
  • I appreciate this answer, but the question was about `pyenv` explicitly, since `pyenv` doesn't put virtual environments in the directory. – pixelearth Apr 05 '23 at 13:27