Related
Working inside a vagrant environment, inside a python virtual environment, when I try to install a python package using
(venv) vagrant#vagrant-ubuntu-trusty-64:~$ pip install <package_name>
I receive a permission error:
error: could not create '/home/vagrant/venv/lib/python2.7/site-packages/<package_name>': Permission denied
When I use sudo to install:
(venv) vagrant#vagrant-ubuntu-trusty-64:~$ sudo pip install <package_name>
the install is successful, but the package is not installed inside venv, but instead inside the global python directory.
I can successfully install the package inside venv by using sudo and specifying the path to pip:
(venv) vagrant#vagrant-ubuntu-trusty-64:~$ sudo /home/vagrant/venv/bin/pip install <package_name>
This is quite convoluted though. So how can I stop sudo pip linking to the global python pip?
Thank you
I had the same problem with pip vs sudo pip and virtualenv pip vs local pip.
I was logged in as root user when I created my venv months ago. So when I wanted to install a new pip package got permission denied. So tried the same command with sudo, but then it installed the package on my local pip.
Lesson learned. I should not use sudo inside a venv.
Fixed it with:
chmod -R 0777 venv_folder_path_here
-R switch for recursive change in venv folder.
And then activate your venv and try pip install:
/home/username_here/venv/project_name_here/bin/activate
(venv_name) pip install package_name_here
The root problem is that sudo does not by default inherit the user's environment as it executes the command. This is what you want - trust me on this.
In your case, your pip is either guided to the venv that it can't write to or - under sudo - to root's environment where you don't want it to be.
The solution you posted is actually valid: If you use sudo, be sure to tell it exactly what to do, how to do it and whom to do it to! All of the aforementioned can be controlled by the user's environment variables so caution is key.
You may also use sudo -E, which does inherit the calling user's environment and should therefore preserve your venv. Be sure to read sudo's man-page or do some googling about all the possible trouble you could get in, though.
Like Daniel said in comments, you should fix the permissions issue with your virtual environment directory. It could be that you already installed something in that directory with sudo. Or you created it with sudo. Which is not ideal. I recommend destroying the virtualenv and then creating it again with the vagrant user. If you are using pyvenv, make sure you pass --copies option.
As user27... said in their answer, the pip you run with sudo is probably not the same pip you run as vagrant user. You can always check that with which pip.
I'd recommend starting with which python inside your python virtual environment. Perhaps you have activated the wrong virtual environment, not related to your vagrant user at all.
I am trying to create a virtual environment using virtualenv on Mac OS X El Capitan. I have installed Python 2.7.11 with brew, which includes pip, wheel and setuptools by default.
Hovewer, when I try to install virtualenv following instructions in the documentation or from any other resource, I get several problems:
virtualenv executable is not placed in /usr/local/bin after pip makes its job, so I need to ln -s it by hand (it may indicate, that there is something wrong with installation on this step).
After I run virtualenv venv, it creates new environment, catches Python 2.7.11 from brew-installation, but: there is no pip inside bin folder. That means, that if I try which pip, having venv activated, it returns a global position of pip — /usr/local/bin/pip, not /path/to/venv/bin/pip.
As a consequence, installing packages inside venv uses global pip and installs them to a global sites-packages, not that inside venv, and it's quite the opposite of what environment should do.
Could you please suggest what may be wrong and how to fix it?
EDIT: The thing to mention is that I used to have other versions of Python installed on my computer, which I have recently deleted as it is described in this answer. Maybe it causes the issue, and some more thorough cleaning is needed.
Try removing or renaming the .pydistutils.cfg file in your home directory, e.g. by renaming with mv ~/.pydistutils.cfg ~/oldpydistutils.cfg
I'm putting a detailed answer here to help others, but the original credit goes to this answer. If you know what specifically in .pydistutils.cfg was causing the problem, let me know!
I was having the same issue: my virtual environments were created without a local copy of pip, although they had a local copy of python. This meant that using $ pip from within the virtual environment installed to the global package location, and was not visible to the environment's python.
How I diagnosed this on my machine:
I create a virtualenvironment with $ virtualenv env
Activated the virtual environment with $ source env/bin/activate
Checked python location: run (env)$ which python with output /Users/<username>/env/bin/python (as expected)
Checked pip location: run (env)$ which pip with output /usr/local/bin/pip (NOT expected)
To check where our packages are going, we can try to install a package in the virtual environment:
Try to install a package: (env)$ pip install HTTPServer which succeeds
Try to run the package: (env)$ python -m HTTPServer which fails with error /Users/emunsing/env/bin/python: No module named HTTPServer
To double-check, try to install again: (env)$ pip install HTTPServer which produces Requirement already satisfied (use --upgrade to upgrade): HTTPServer in /usr/local/lib/python2.7/site-packages
Double-checking, we see that there's no Pip in the environment's /bin folder:
$ ls env/bin
activate activate.fish python python2
activate.csh activate_this.py python-config python2.7
And so while the system finds the local python version, it can't find a local pip to use and traverses the $PATH. It ended up using pip from /usr/local/bin, leaving me unable to install packages locally to the virtual environment.
Here's what I tried:
- Reinstalling python brew uninstall python followed by brew upgrade and brew install python --build-from-source
- Installing pip using the get-pip.py command as described in the Pip documentation
Here's what I ruled out:
- I was not using sudo pip ... which caused similar problems in this other question and haven't done so at any time on this Python/pip install
- My virtual environment didn't show a local installation of pip, as was the case in these similar questions: This one for Windows, This one for Mac OS X.
Ultimately, I found that eliminating the ~/.pydistutils.cfg file fixed the problem, allowing for fresh virtual environments that had their own local pip. The contents of my ~/.pydistutils.cfg file were:
[global]
verbose=1
[install]
install-scripts=$HOME/bin
[easy_install]
install-scripts=$HOME/bin
Simply renaming the ~/.pydistutils.cfg file appears to fix the problem: it seems that although this file was created by the homebrew installation, some settings in this file may be incompatible with virtualenv. While removing this file hasn't had any bad effects on my system, you may need to use the --user flag when installing packages with pip to the global environment (e.g. $ pip install --user HTTPServer). Here are more details on .pydistutils.cfg if you want to work on tailoring it for your needs.
virtualenv executable is not placed in /usr/local/bin after pip makes its job, so I need to ln -s it by hand (it may indicate, that there is something wrong with installation on this step).
Don't do that. That will only hide the bug and not solve the problem. Here's a short guide how to debug this kind of issues:
Start with which -a python. The first path you see should be /usr/local/bin/python, if not check your PATH variable.
Next, check which -a pip. Again the first path should be /usr/local/bin/pip. If not, run python -m ensurepip and recheck.
Now install virtualenv using pip install virtualenv, after that check the output of which -a virtualenv. The first path should be /usr/local/bin/virtualenv, if not check the output of env |grep PYTHON for unexpected environment variables.
Finally check the output of virtualenv --version to make sure you have the latest version.
I had the issue when running virtualenv: "ImportError: No module named pip."
My solution was to downgrade virtualenv. I had 16.2.0.
pip uninstall virtualenv
pip install virtualenv==15.1.0
Just hit same issue on Linux. Seems like there are multiple causes of this issue, but for me answer was to remove ~/.pip/.
Details: I had this in my .pip/pip.conf for some reason I can't remember:
$ cat ~/.pip/pip.conf
[global]
use_https = True
index = https://pypi.python.org/pypi
prefix = /home/sam/local/
and was using local versions on Python, Pip installed at ~/local/. For some reason virtualenv installed pip must pick up prefix = /home/sam/local/ setting and pip was being installed there.
Try this: sudo apt install pythonV.v-distutils.
In my case V.v == 3.8.
This worked for me.
I couldn't get virtualenv to work despite various attempts. I installed virtualenv on MAC OS X using:
pip install virtualenv
and have also added the PATH into my .bash_profile. Every time I try to run the virtualenv command, it returns:
-bash: virtualenv: command not found
Every time I run pip install virtualenv, it returns:
Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
I understand that in mac, the virtualenv should be correctly installed in
/usr/local/bin
The virtualenv is indeed installed in /usr/local/bin, but whenever I try to run the virtualenv command, the command is not found. I've also tried to run the virtualenv command in the directory /usr/local/bin, and it gives me the same result:
-bash: virtualenv: command not found
These are the PATHs I added to my .bash_profile
export PATH=$PATH:/usr/local/bin
export PATH=$PATH:/usr/local/bin/python
export PATH=$PATH:/Library/Framework/Python.framework/Version/2.7/lib/site-packages
Any workarounds for this? Why is this the case?
If you installed it with
pip install virtualenv
You need to run
sudo /usr/bin/easy_install virtualenv
which puts it in /usr/local/bin/.
The above directory by default should be in your PATH; otherwise, edit your .zshrc (or .bashrc) accordingly.
I faced the same issue and this is how I solved it:
The issue occurred to me because I installed virtualenv via pip as a regular user (not root). pip installed the packages into the directory ~/.local/lib/pythonX.X/site-packages
When I ran pip as root or with admin privileges (sudo), it installed packages in /usr/lib/pythonX.X/dist-packages. This path might be different for you.
virtualenv command gets recognized only in the second scenario
So, to solve the issue, do pip uninstall virtualenv and then reinstall it with sudo pip install virtualenv (or install as root)
The simplest answer. Just:
pip uninstall virtualenv
and then:
pip install virtualenv
Or you maybe installed virtualenv with sudo, in that case:
pip install --user virtualenv
python3 -m virtualenv virtualenv_name
or
python -m virtualenv virtualenv_name
On Ubuntu 18.04 LTS I also faced same error.
Following command worked:
sudo apt-get install python-virtualenv
I had the same issue. I used the following steps to make it work
sudo pip uninstall virtualenv
sudo -H pip install virtualenv
That is it. It started working.
Usage of sudo -H----> sudo -H: set HOME variable to target user's home dir.
I had same problem on Mac OS X El Capitan.
When I installed virtualenv like that sudo pip3 install virtualenv I didn't have virtualenv under my command line.
I solved this problem by following those steps:
Uninstall previous installations.
Switch to super user account prior to virtualenv installation by calling sudo su
Install virtualenv by calling pip3 install virtualenv
Finally you should be able to access virtualenv from both user and super user account.
Figure out the problem
Try installing with the --verbose flag
pip install virtualenv --verbose
Output will look something like this
..
Using cached virtualenv-15.1.0-py2.py3-none-any.whl
Downloading from URL https://pypi.python.org/packages/6f/86/3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/virtualenv-15.1.0-py2.py3-none-any.whl#md5=aa7e5b86cc8cdb99794c4b99e8d670f3 (from https://pypi.python.org/simple/virtualenv/)
Installing collected packages: virtualenv
changing mode of /home/manos/.local/bin/virtualenv to 755
Successfully installed virtualenv-15.1.0
Cleaning up...
From the output we can see that it's installed at /home/manos/.local/bin/virtualenv so let's ensure PATH includes that.
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
In my case we can clearly see that /home/manos/.local/bin is totally missing and that's why the shell can't find the program.
Solutions
We can solve this in many ways:
We can install directly to a specific directory by fiddling with pip options (not recomended).
Create appropriate symlinks at /usr/local/bin or similar.
Append /home/manos/.local/bin to PATH.
Install as sudo to install directly to /usr/local/bin
The two last options are probably the most sensible. The last solution is the simplest so therefore I will just show solution 3.
Add this to ~/.profile:
PATH="$PATH:$HOME/.local/bin"
Logout out and in again and it should work.
Found this solution and this worked perfectly for me.
sudo -H pip install virtualenv
The -H sets it to the HOME directory, which seems to be the issue for most people.
Personally. I did the same steps you did on a fresh Ubuntu 20 installation (except that I used pip3). I got the same problem, and I remember I solved it this way:
python3 -m virtualenv venv
Link to understand the -m <module-name> notation.
You are having this error :
zsh: command not found: virtualenv
Because most probably you tried to install virtualenv without typing sudo beforehand.
If you try to add it to /usr/local/bin, this may result on syntax errors as the packages are not properly isntalled/copied:
SyntaxError: invalid syntax
File "build/bdist.macosx-12.0-x86_64/egg/platformdirs/__main__.py", line 16
def main() -> None:
^
In case you have tried to install virtualenv via pip without sudo rights, you need first to uninstall it:
pip3 uninstall virtualenv
Then install it using sudo:
sudo pip3 install virtualenv
Next you just need to activate the env:
virtualenv env
source env/bin/activate
In my case, I ran pip show virtualenv to get the information about virtualenv package. I will look similar to this and will also show location of the package:
user#machine:~$ pip show virtualenv
Name: virtualenv
Version: 16.2.0
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Ian Bicking
Author-email: ianb#colorstudy.com
License: MIT
Location: /home/user/.local/lib/python3.6/site-packages
Requires: setuptools
From that grab the part of location up to the .local part, which in this case is /home/user/.local/. You can find virtualenv command under /home/user/.local/bin/virtualenv.
You can then run commands like /home/user/.local/bin/virtualenv newvirtualenv.
You said that every time you run the pip install you get Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. What you need to do is the following:
Change Directory (go to to the one where the virtualenv.py)
cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
If you do an ls you will see that the script is there virtualenv.py
Run the script like this:
python virtualenv.py --distribute /the/path/at/which/you/want/the/new/venv/at theNameOfTheNewVirtualEnv
Hope this helps. My advice would be to research venvs more. Here is a good resource: https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/
I had troubles because I used apt to install python-virtualenv package.
To get it working I had to remove this package with apt-get remove python-virtualenv and install it with pip install virtualenv.
Ensure that virtualenv is executable.
If virtualenv is not found, running the full path (/usr/local/bin/virtualenv) should work.
I had the same problem for a long time.
I solved it by running these two commands, first is to install second is to activate the env:
python3 -m pip install virtualenv
python3 -m virtualenv yourenvname
Note that I'm using python3, you can change it to just python if python3 fails.
Thanks.
I think your problem can be solved using a simple symbolic link, but you are creating the symbolic link to the wrong file. As far as I know virtualenv is installed to /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv, (you can change the numbers for your Python version) so the command for creating the symbolic link should be:
ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv /usr/local/bin/virtualenv
On ubuntu 18.4 on AWS installation with pip don't work correctly.
Using apt-get install the problem was solved for me.
sudo apt-get install python-virtualenv
and to check
virtualenv --version
Same problem:
So I just did pip uninstall virtualenv
Then pip install virtualenv
pip install virtualenv --user
Collecting virtualenv
Using cached https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl
Installing collected packages: virtualenv
Then I got this :
The script virtualenv is installed in '/Users/brahim/Library/Python/2.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
which clearly says where it is installed and what to do to get it
If you're using Linux, open your terminal and type virtualenv halfway and autocomplete with tab key. If there's no auto-completion install virtualenv on your system by running:
mycomp$sudo apt-get install virtualenv
//if you're already super user.
mycomp#apt-get install virtualenv
You can now navigate to where you want to create your project and do:
myprj$pip3 install virtualenv
//to install python 3.5 and above
myprj$virtualenv venv --python=python3.5
//to activate virtualenv
(venv)myprj$source venv/bin/activate
(venv)myprj$deactivate
This works on Ubuntu 18 and above (not tested in previous versions):
sudo apt install python3-virtualenv
Make sure that you are using
sudo
In this case, at first you need to uninstall the pipenv and then install again using sudo command.
pip uninstall pipenv
sudo pip install pipenv
Follow these basic steps to setup the virtual env
sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip
we need to update our ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
The ~/.bashrc file is simply a shell script that Bash runs whenever you launch a new terminal. You normally use this file to set various configurations. In this case, we are setting an environment variable called WORKON_HOME to point to the directory where our Python virtual environments live. We then load any necessary configurations from virtualenvwrapper .
To update your ~/.bashrc file simply use a standard text editor, nano is likely the easiest to operate.
A more simple solution is to use the cat command and avoid editors entirely:
echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
After editing our ~/.bashrc file, we need to reload the changes:
source ~/.bashrc
Now that we have installed virtualenv and virtualenvwrapper , the next step is to actually create the Python virtual environment — we do this using the mkvirtualenv command.
mkvirtualenv YOURENV
I'm doing Angela Yu's online iOS course and I was getting same problem plus also was getting permission denied error 13 when I was trying to run virtualenv --python=/{myPath} {newVirtualEnvName}
I solved it by:
switching to sudo user sudo su
navigating to my destination folder (where I want my new virtual env to live) ie. /Users/muUserName/Environments/
run command python -m virtualenv python27 where python27 is a name of my new virtual environment
above created folder pathon27 in my Environments folder, and then I was able to run source python27/bin/activate to start my virtualenv
After upgrading MacOS Monterey from 12.5.1 to 12.6, I was no longer able to run virtualenv. Since I had brew on my mac, installed like this:
$ brew install virtualenv
...
==> Installing virtualenv
==> Pouring virtualenv--20.16.5.monterey.bottle.tar.gz
/usr/local/Cellar/virtualenv/20.16.5: 949 files, 20.3MB
==> Running `brew cleanup virtualenv`...
...
Of course, brew decided to upgrade various other packages I had as well, but virtualenv was available again thereafter.
For me it was installed in this path (python 2.7 on MacOS):
$HOME/Library/Python/2.7/bin
Simple answer is that if you are not a sudo user as I was not one.You need to add path of your bin folder (/home/myusername/.local/bin).So basically the command line searches in which of these path is the command which you have typed.
export PATH=/home/b18150/.local/bin:/usr/bin:/bin
here it will search in local/bin first then /usr/bin and then /bin.
on Mac
> pip3 install virtualenv
> python3 -m virtualenv [venv_name_you_want]
Q. virtualenv not found
After installing virtualenv, virtualenv exist on the pip3 list. But When to use the "virtualenv [venv_name]" command, it returns "virtualenv not found".
A. Because virtualenv is installed as a module in python3. Not installed as a command tool like python3 in the "/usr/bin/.." path. So this case we can use "python3 -m virtualenv [venv_name]".
And we can see where it is to retry this command "pip3 install virtualenv". Then zsh or your shell tells us kindly this info.
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: virtualenv in /Users/[your-usr-name-here]/Library/Python/3.8/lib/python/site-packages (20.16.5)
Requirement already satisfied: filelock<4,>=3.4.1 in /Users/[your-usr-name-here]/Library/Python/3.8/lib/python/site-packages (from virtualenv) (3.8.0) ...
If you installed it with
pip install virtualenv
Now to use it you need to type this command:
python -m virtualenv name_of_your_virtualenv
in order to activate it:
.\name_of_your_virtualenv\Scripts\activate
if you face a problem activating your virtualenv, it could be Execution Policy Settings. To fix it, you should try executing this command in your command line: Set-ExecutionPolicy Unrestricted -Scope Process. This would allow PowerShell will run the script.
apt update
apt upgrade
apt install ufw python virtualenv git unzip pv
3 commands and everything working!
I would like to install the python-numpy in the Virtualenv environment. My system is Ubuntu 12.04, and my python is 2.7.5. First I installed the Virtualenv by
$ sudo apt-get install python-virtualenv
And then set up an environment by
$ mkdir myproject
$ cd myproject
$ virtualenv venv
New python executable in venv/bin/python
Installing distribute............done.
Activated it by
$ . venv/bin/activate
Installed python-numpy in the environment by
$ sudo apt-get install python-numpy
However, I tried to import numpy in python in the environment after all steps above. Python told me "No modules named numpy". Whereas, numpy could be imported in Python globally. I tried to remove and install many times but it does not work. I am a beginner of both Python and Linux.
apt-get will still install modules globally, even when you're in your new virtualenv.
You should either use pip install numpy from within your virtual environment (easiest way), or else compile and install numpy from source using the setup.py file in the root of the source directory (slightly harder way, see here).
I'd also thoroughly recommend you take a look at virtualenvwrapper, which makes managing virtual environments much friendlier.
Edit:
You should not be using sudo, either to create your virtual environment or to install things within it - it's a directory in your home folder, you don't need elevated permissions to make changes to it. If you use sudo, pip will make changes to your global site packages, not to your virtual environment, hence why you weren't able to install numpy locally.
Another thing to consider is that by default, new virtualenvs will inherit from the global site-packages - i.e. if Python can't find a module locally within your virtualenv, Python will also look in your global site packages *. In your case, since you'd already installed numpy globally (using apt-get), when you then try to pip install numpy in your virtual environment, pip sees that numpy is already in your Python path and doesn't install it locally.
You could:
Pass the --no-site-packages option when you create your virtualenv. This prevents the new virtualenv from inheriting from the global site packages, so everything must be installed locally.
Force pip to install/upgrade numpy locally, e.g. using pip install -U --force numpy
* As of v1.7, the default behaviour of virtualenv is to not include the global site-packages directory. You can override this by passing the --system-site-packages flag when creating a new virtual environment.
meddling with PYTHONPATH for site-packages indeed defeats the purpose of virtalenv. what worked for me was to specify the env i wanted the packages to be installed in via pip
example:
pip -E /home/proj1
where proj1 was created using virtualenv.
reference: how to install numpy in a virtualenv
I've created folder and initialized a virtualenv instance in it.
$ mkdir myproject
$ cd myproject
$ virtualenv env
When I run (env)$ pip freeze, it shows the installed packages as it should.
Now I want to rename myproject/ to project/.
$ mv myproject/ project/
However, now when I run
$ . env/bin/activate
(env)$ pip freeze
it says pip is not installed. How do I rename the project folder without breaking the environment?
You need to adjust your install to use relative paths. virtualenv provides for this with the --relocatable option. From the docs:
Normally environments are tied to a
specific path. That means that you
cannot move an environment around or
copy it to another computer. You can
fix up an environment to make it
relocatable with the command:
$ virtualenv --relocatable ENV
NOTE: ENV is the name of the virtual environment and you must run this from outside the ENV directory.
This will make some of the files
created by setuptools or distribute
use relative paths, and will change
all the scripts to use
activate_this.py instead of using the
location of the Python interpreter to
select the environment.
Note: you must run this after you've
installed any packages into the
environment. If you make an
environment relocatable, then install
a new package, you must run virtualenv
--relocatable again.
I believe "knowing why" matters more than "knowing how". So, here is another approach to fix this.
When you run . env/bin/activate, it actually executes the following commands (using /tmp for example):
VIRTUAL_ENV="/tmp/myproject/env"
export VIRTUAL_ENV
However, you have just renamed myproject to project, so that command failed to execute.
That is why it says pip is not installed, because you haven't installed pip in the system global environment and your virtualenv pip is not sourced correctly.
If you want to fix this manually, this is the way:
With your favorite editor like Vim, modify /tmp/project/env/bin/activate usually in line 42:
VIRTUAL_ENV='/tmp/myproject/env' => VIRTUAL_ENV='/tmp/project/env'
Modify /tmp/project/env/bin/pip in line 1:
#!/tmp/myproject/env/bin/python => #!/tmp/project/env/bin/python
After that, activate your virtual environment env again, and you will see your pip has come back again.
NOTE: As #jb. points out, this solution only applies to easily (re)created virtualenvs. If an environment takes several hours to install this solution is not recommended
Virtualenvs are great because they are easy to make and switch around; they keep you from getting locked into a single configuration. If you know the project requirements, or can get them, Make a new virtualenv:
Create a requirements.txt file
(env)$ pip freeze > requirements.txt
If you can't create the requirements.txt file, check env/lib/pythonX.X/site-packages before removing the original env.
Delete the existing (env)
deactivate && rm -rf env
Create a new virtualenv, activate it, and install requirements
virtualenv env && . env/bin/activate && pip install -r requirements.txt
Alternatively, use virtualenvwrapper to make things a little easier as all virtualenvs are kept in a centralized location
$(old-venv) pip freeze > temp-reqs.txt
$(old-venv) deactivate
$ mkvirtualenv new-venv
$(new-venv) pip install -r temp-reqs.txt
$(new-venv) rmvirtualenv old-venv
I always install virtualenvwrapper to help out. From the shell prompt:
pip install virtualenvwrapper
There is a way documented in the virtualenvwrapper documents - cpvirtualenv
This is what you do. Make sure you are out of your environment and back to the shell prompt. Type in this with the names required:
cpvirtualenv oldenv newenv
And then, if necessary:
rmvirtualenv oldenv
To go to your newenv:
workon newenv
You can fix your issue by following these steps:
rename your directory
rerun this: $ virtualenv ..\path\renamed_directory
virtualenv will correct the directory associations while leaving your packages in place
$ scripts/activate
$ pip freeze to verify your packages are in place
An important caveat, if you have any static path dependencies in script files in your virtualenv directory, you will have to manually change those.
Yet another way to do it that worked for me many times without problems is virtualenv-clone:
pip install virtualenv-clone
virtualenv-clone old-dir/env new-dir/env
Run this inside your project folder:
cd bin
sed -i 's/old_dir_name/new_dir_name/g' *
Don't forget to deactivate and activate.
In Python 3.3+ with built-in venv
As of Python 3.3 the virtualenv package is now built-in to Python as the venv module. There are a few minor differences, one of which is the --relocatable option has been removed. As a result, it is normally best to recreate a virtual environment rather than attempt to move it. See this answer for more information on how to do that.
What is the purpose behind wanting to move rather than just recreate any virtual environment? A virtual environment is intended to manage the dependencies of a module/package with the venv so that it can have different and specific versions of a given package or module it is dependent on, and allow a location for those things to be installed locally.
As a result, a package should provide a way to recreate the venv from scratch. Typically this is done with a requirements.txt file and sometimes also a requirements-dev.txt file, and even a script to recreate the venv in the setup/install of the package itself.
One part that may give headaches is that you may need a particular version of Python as the executable, which is difficult to automate, if not already present. However, when recreating an existing virtual environment, one can simply run python from the existing venv when creating the new one. After that it is typically just a matter of using pip to reinstall all dependencies from the requirements.txt file:
From Git Bash on Windows:
python -m venv mynewvenv
source myvenv/Scripts/activate
pip install -r requirements.txt
It can get a bit more involved if you have several local dependencies from other locally developed packages, because you may need to update local absolute paths, etc. - though if you set them up as proper Python packages, you can install from a git repo, and thus avoid this issue by having a static URL as the source.
virtualenv --relocatable ENV is not a desirable solution. I assume most people want the ability to rename a virtualenv without any long-term side effects.
So I've created a simple tool to do just that. The project page for virtualenv-mv outlines it in a bit more detail, but essentially you can use virtualenv-mv just like you'd use a simple implementation of mv (without any options).
For example:
virtualenv-mv myproject project
Please note however that I just hacked this up. It could break under unusual circumstances (e.g. symlinked virtualenvs) so please be careful (back up what you can't afford to lose) and let me know if you encounter any problems.
Even easier solution which worked for me: just copy the site-packages folder of your old virtual environment into a new one.
Using Visual Studio Code (vscode), I just opened the ./env folder in my project root, and did a bulk find/replace to switch to my updated project name. This resolved the issue.
Confirm with which python
If you are using an conda env,
conda create --name new_name --clone old_name
conda remove --name old_name --all # or its alias: `conda env remove --name old_name`