4

When I run python -m venv, the virtual environment directory that venv creates includes a binary named python and another named python3 which is just a link to python. (In my installation, python is Python 3.6 and python2 is Python 2.7.)

My problem is, sometimes (and I can't understand what's the difference between subsequent invocations) it also creates another symlink python3.6 pointing to python, but sometimes it doesn't. I need this symlink (actually, tox needs it). The binaries pip3.6 and easy_install-3.6 are always installed in the virtualenv.

Is there any way I can make sure that python -m venv creates a symlink python3.6?

(Disclaimer: I'm using pyenv to manage my Python installation, but I can reproduce the behavior above using /usr/bin/python -m venv)

fonini
  • 2,989
  • 3
  • 21
  • 39
  • 1
    Generally, I use "python3.6 -m venv myvenv". This distinguishes it from any other python 3.x I may have installed. Don't know if that address your issue, but maybe. – SteveJ Mar 08 '18 at 16:02
  • I am sure that the python version being used is 3.6, because it installs `pip3.6` and `easy_install-3.6`, but I'll try – fonini Mar 08 '18 at 16:03
  • It worked! Apparently, python uses the version number in the `$0` argument to decide what binary versions it will install. Thanks! – fonini Mar 08 '18 at 16:05
  • For completeness, I've added this as an answer. – SteveJ Mar 08 '18 at 16:11

2 Answers2

4

When creating venvs (python -m venv, not virtualenv), I've had success by including the version number in the call to create;

python3.6 -m venv myvenv
fonini
  • 2,989
  • 3
  • 21
  • 39
SteveJ
  • 3,034
  • 2
  • 27
  • 47
  • if `venv` has this issues.. what is the benefit of useing `venv` over `virtualenv`? – alpha_989 Mar 08 '18 at 16:20
  • Also for completeness: [this search](https://github.com/pyenv/pyenv-virtualenv/issues?utf8=%E2%9C%93&q=is%3Aissue+tox) in [pyenv-virtualenv](https://github.com/pyenv/pyenv-virtualenv)'s github issues brings up [this solution](https://github.com/pyenv/pyenv-virtualenv/issues/202#issuecomment-284728205), which I've found very satisfying: when creating the virtualenv with `pyenv virtualenv`, use the `-p` flag to specify which binary pyenv-virtualenv should use when calling `python -m venv`. The final, complete command will be something like `pyenv virtualenv -p python3.6 3.6.4 myvenv`. – fonini Mar 08 '18 at 16:25
  • 1
    @alpha_989; See the answer from Riaz in this SO question; https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe – SteveJ Mar 08 '18 at 16:32
2

You need to have the binary already available to get venv to use it, but if you have it, it shouldn't matter if you use python2 -m venv or python3 -m venv. If you want 3.6, try:

python -m venv python=`which python3.6` ~/envs/py36

ThisGuyCantEven
  • 1,095
  • 12
  • 21