can't create virtual environment using pyenv on macos

Viewed 532

I tried to create virtual environment using pyenv virtualenv 3.8.2 myenv, but it failed, i don't know why, i even changed the python version or the virtual environment name, but it still not working.

Some detail:

I tried this in macos and zsh.

➜  ~ pyenv virtualenv 3.8.2 myenv
Looking in links: /var/folders/_9/l8m14fgs6ts9wx0nl1qbzbkm0000gn/T/tmpe9l25o2_
Requirement already satisfied: setuptools in /Users/fitz/.pyenv/versions/3.8.2/envs/myenv/lib/python3.8/site-packages (41.2.0)
Requirement already satisfied: pip in /Users/fitz/.pyenv/versions/3.8.2/envs/myenv/lib/python3.8/site-packages (19.2.3)
rm: /Users/fitz/.pyenv/shims/shims: is a directory

➜  ~ pyenv versions
  system
  3.7.3
* 3.8.2 (set by /Users/fitz/.python-version)
  3.9.5
2 Answers

Try using 'venv' instead of pyenv or pyvenv or virtualenv. Venv is a library that already comes with your python installation. Virtualenv is an external one.pyenv is similar to venv in that it lets you manage multiple python environments. However with pyenv you can't conveniently rollback library installs to some start state and you will likely need admin privileges at some point to update libraries. So I think it is also best to use venv.

  1. First, make a directory :
mkdir testing
  1. Then, moved to this directory named testing :
cd testing
  1. When you type following command in this directory:
python3 -m venv env

You got error like :

The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command.

apt install python3.8-venv
  1. Type the following command but before that keep an eye on the version of python you installed on the machine; in my case its python3.8
sudo apt install python3.8-venv
  1. Now, we can create a virtual environment and store its tools in the "bhandari" folder .
python3 -m venv bhandari   

Note: you can named this "bhandari" folder; anyname you like( Standard practice is to name it "env" ...)

  1. Now to activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “bhandari” folder
source bhandari/bin/activate
  1. If you have successfully activated your virtual environment, you should see the (bhandari) word indicating that we are working in a virtual environment.

After this, we can install anything that will be isolated from the rest of the system....

I had the same issue. For me it was caused by two empty folders in my shims directory. I deleted the folders and it fixed the issue. So, just running the following would fix your issue.

rm -rf ~/.pyenv/shims/shims
rm -rf ~/.pyenv/shims/versions
Related