How do you create a virtualenv using python installed by pyenv

3k views Asked by At

I have 2 pyenv versions installed

pyenv versions
  system
* 2.7 (set by ../.python-version)
  3.5.3

when I am creating a virtualenv inside my project directory, I want it to create a virtualenv for the current pyenv python version 2.7

but when i'm creating one using mkvirtualenv whatever

the virtual env is creating a python3 virtualenv

ll ~/.virtualenvs/mobile2/bin
total 5800
-rw-r--r--  1 ohadperry  staff   2.0K Sep  6 10:59 activate
-rw-r--r--  1 ohadperry  staff   1.0K Sep  6 10:59 activate.csh
-rw-r--r--  1 ohadperry  staff   2.1K Sep  6 10:59 activate.fish
-rw-r--r--  1 ohadperry  staff   1.1K Sep  6 10:59 activate_this.py
-rwxr-xr-x  1 ohadperry  staff   266B Sep  6 10:59 easy_install
-rwxr-xr-x  1 ohadperry  staff   266B Sep  6 10:59 easy_install-3.5
-rwxr-xr-x  1 ohadperry  staff   149B Sep  6 10:59 get_env_details
-rwxr-xr-x  1 ohadperry  staff   238B Sep  6 10:59 pip
-rwxr-xr-x  1 ohadperry  staff   238B Sep  6 10:59 pip3
-rwxr-xr-x  1 ohadperry  staff   238B Sep  6 10:59 pip3.5
-rw-r--r--  1 ohadperry  staff    71B Sep  6 10:59 postactivate
-rw-r--r--  1 ohadperry  staff    73B Sep  6 10:59 postdeactivate
-rwxr-xr-x  1 ohadperry  staff    68B Sep  6 10:59 preactivate
-rw-r--r--  1 ohadperry  staff    74B Sep  6 10:59 predeactivate
-rwxr-xr-x  1 ohadperry  staff   2.8M Sep  6 10:58 python
-rwxr-xr-x  1 ohadperry  staff   2.3K Sep  6 10:59 python-config
lrwxr-xr-x  1 ohadperry  staff     6B Sep  6 10:58 python3 -> python
lrwxr-xr-x  1 ohadperry  staff     6B Sep  6 10:58 python3.5 -> python
-rwxr-xr-x  1 ohadperry  staff   245B Sep  6 10:59 wheel
3

There are 3 answers

0
Somil On BEST ANSWER

for python2.7

virtualenv -p /usr/bin/python2.7 my_env
0
danodonovan On

If you're using pyenv, I would recommend building a pyenv controlled virualenv (make sure pyenv-virtualenv is installed first)

$ pyenv virtualenv 2.7 <your new env>

Which you can then switch to this virtualenv using pyenv

$ pyenv local <your new env>

or

$ pyenv shell <your new env>
0
Filipe Nicoli On

I was having a similar problem, and I believe the accepted answer doesn't reflect the question since it's been edited. I do believe my solution solves the stated problem as it is right now.

The original question was how to create a virtualenv in selected python version explicitly, for which Somil's answer is correct.

However, for those who wish to create a virtualenv using a python version installed by pyenv, here is my answer.


I was trying to create a virtualenv using Python 3.6.9, having 3.10.7 installed system-wide. I had no luck using the system installed virtualenv. I tried virtualenv -p ~/.pyenv/versions/<version>/bin/python env, and although it creates the folders and scripts, it presented errors regarding wheel packages.

What worked was installing virtualenv from the pyenv installed version and using it to create the env.

  1. Install the Python version you want:
$ pyenv install <version>
  1. Select that version to run on your current shell
$ pyenv shell <version>
  1. Install virtualenv using pip from that version
$ pip3 install virtualenv
  1. Use the installed virtualenv to create the env.
$ virtualenv -p python3 env