0

I am having some trouble with creating virtual environments using pyenv.

This works fine:

$ pyenv local 3.6.0
$ python
>>> Python 3.6.0 (default, Jul 21 2019, 14:03:29)
>>> [GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin
>>> ...

But then when I create a virtual environment (which I assume done with Python 3.6.0) and activate it, I get Python 3.7.4. The same happens if I use python3.6 -m venv venv.

$ python -m venv venv
$ source venv/bin/activate
$ python
>>> Python 3.7.4 (default, Jul  9 2019, 18:13:23)
>>> [Clang 10.0.1 (clang-1001.0.46.4)] on darwin

Putting that into words, I want to create a virtual environment with the same version that is currently active via pyenv. What am I missing here?

Some info to my system:

  • I am on macOS.
  • I have installed Python 3.7.4 via Homebrew, and it's the default version for python3.
  • If I call python only without having activated pyenv in my shell, I get Python 2.7.16 interpreter.
D Pinto
  • 871
  • 9
  • 27

2 Answers2

0

If I call python only without having activated pyenv in my shell, I get Python 2.7.16 interpreter.

This means shims from pyenv is not added into the PATH environment.

pyenv need to be initialized in your shell conf with following code,

if command -v pyenv &>/dev/null; then
  eval "$(pyenv init -)"
fi

if command -v pyenv-virtualenv &>/dev/null; then
  "$(pyenv virtualenv-init -)"
fi

After that, restart your shell. pyenv will work as expected.

Check basic setup from pyenv from the project's repo for more info.

Simba
  • 23,537
  • 7
  • 64
  • 76
-2

I wrote about it here, and what you should do is:

virtualenv -p /path/to/your/python/version/exectuable ENV
A K
  • 788
  • 6
  • 17
  • I don't have virtualenv installed. Can the same be accomplished with the built-in venv module? – D Pinto Jul 29 '19 at 11:43
  • This doesn't answer the question, which is why venv isn't behaving as expected. – Derlin Jul 29 '19 at 11:44
  • @DPinto - yes, you can use this with venv module, as I know. – A K Jul 29 '19 at 11:56
  • @Derlin - no. the question was "I want to create a virtual environment with the same version that is currently active", meaning "how" and not "why". But for the "why" question - I think that venv take the executable from the path, and that way you can overwrite it. – A K Jul 29 '19 at 11:59