Using virtualenvwrapper and pyenv together
Asked Answered
S

1

7

$ pyenv virtualenv 3.8.0 tf12 creates a virtualenv located in ~/.pyenv/versions/3.8.0/envs/tf12 which contains packages we installed into it using pip install. If we create a new project directory like mkdir myfolder && cd myfolder && pyenv local tf12, that project directory will use the same kernels and packages that the tf12 environment contains because we used the pyenv local command.

But then we also have virtualenvs and directories created with mkproject mynewenv located somewhere like ~/.ve and ~/workspace. The workspace is where we place notebooks, code and scripts .pynb, .py, .r etc and the corresponding virtualenv uses the global python version that was active when executing mkproject mynewenv.

These virtualenvs created with mkproject mynewenv are separate from virtualenvs created with pyenv virtualenv.

I have come to the conclusion that we cannot use them together for further possibilities. They are used independently. Correct me if I'm wrong.

Sandbag answered 8/5, 2020 at 9:5 Comment(1)
I keep reading tutorials about how to setup both and no one ever really says you can or can't it is always sorta implied that it should be possible. I'm with you on this, it feels like they are very different. Even trying to use pyenv-virtualenvwrapper doesn't seem to work like I think it should.Culpepper
S
3

You should install pyenv-virtualenvwrapper plugin and set it up. After that you can set up the python version and then create a virtual env.

pyenv local 3.8.0
mkvirtualenv test-venv

You can create a shell function to condense those two lines into one line if you want.

If you don't want to use pyenv local command to avoid creating a .python-version file, you can use pyenv shell <python-version> command instead.

# .bash_profile or .zshrc after pyenv and virtualenvwrapper init.
mkvenv()
{
  pyenv shell $1
  mkvirtualenv $2 ${@:3} 
}

Remember that using mkvirtualenv test-venv -p python<version> won't pick up python versions installed by pyenv.

Another method: If you just want to create a venv with mkvirtualenv, you can use a shell function to replace it's behavior.

# .bash_profile or .zshrc after virtualenvwrapper init.
pyvenv()
{
  python$1 -m venv $WORKON_HOME/$2
  workon $2
}

To create virtualenv, use pyvenv <python-version> <venv-name>. You can use all virtualenvwrapper commands with the newly created venv.

Sadiras answered 20/7, 2021 at 0:17 Comment(2)
Sorry. Didn't see your answer all this time lol. Probably cause I resolved it so hadn't looked at SO in a while. I just ended up with using pyenv local after all. Recently using miniforge though.Sandbag
Thought I'd throw this in there. I've always avoided using Anaconda because there's no need to use that bloated pos. Up until recently I was using miniforge's conda or mamba. But now that conda has the libmamba solver, I think there is no reason to use anything else other than conda.Sandbag

© 2022 - 2024 — McMap. All rights reserved.