Inside virtual env, "sudo pip" links to the global python pip - python

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.

Related

Pipenv: Command Not Found

I'm attempting to use pipenv. I ran the command pip install pipenv, which ran successfully:
...
Successfully built pipenv pathlib shutilwhich pythonz-bd virtualenv-clone
Installing collected packages: virtualenv, pathlib, shutilwhich, backports.shutil-get-terminal-size, pythonz-bd, virtualenv-clone, pew, first, six, click, pip-tools, certifi, chardet, idna, urllib3, requests, pipenv
...
However, when I run the command pipenv install in a fresh root project directory I receive the following message: -bash: pipenv: command not found. I suspect that I might need to modify my .bashrc, but I'm unclear about what to add to the file or if modification is even necessary.
This fixed it for me:
sudo -H pip install -U pipenv
That happens because you are not installing it globally (system wide). For it to be available in your path you need to install it using sudo, like this:
$ sudo pip install pipenv
If you've done a user installation, you'll need to add the right folder to your PATH variable.
PYTHON_BIN_PATH="$(python3 -m site --user-base)/bin"
PATH="$PATH:$PYTHON_BIN_PATH"
See pipenv's installation instructions
I tried this:
python -m pipenv # for python2
python3 -m pipenv # for python3
Why this works: In Bash and other Unix-like shell environments, the -m option is used to specify a module to be run as a script.
When you run a Python script using the python -m command, you are telling the Python interpreter to execute the script as if it were a top-level module, rather than as a script file. The python -m pipenv command tells the Python interpreter to run the pipenv module as a script. The pipenv module must be importable from the current working directory or from one of the directories in the PYTHONPATH environment variable.
I have same problem with pipenv on Mac OS X 10.13 High Seirra, another Mac works just fine. I use Heroku to deploy my Django servers, some in 2.7 and some in 3.6. So, I need both 2.7 and 3.6. When HomeBrew install Python, it keeps python points to original 2.7, and python3 points to 3.6.
The problem might due to $ pip install pipenv. I checked /usr/local/bin and pipenv isn't there. So, I tried a full uninstall:
$ pip uninstall pipenv
Cannot uninstall requirement pipenv, not installed
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
$ pip3 uninstall pipenv
Skipping pipenv as it is not installed.
Then reinstall and works now:
$ pip3 install pipenv
Collecting pipenv
Where Python store packages
Before jumping into the command that will install pipenv, it is worth understanding where pip installs Python packages.
Global site-packages is where Python installs packages that will be available to all users and all Python applications on the system. You can check the global site package with the command
python -m site
For example, on Linux with Python 3.7 the path is usually
/usr/lib/python3.7/dist-packages/setuptools
User site-packages is where Python installs packages available only for you. But the packages will still be visible to all Python projects that you create. You can get the path with
python -m site --user-base
On Linux with Python 3.7 the path is usually
~/.local/lib/python3.7/site-packages
Using Python 3.x
On most Linux and other Unices, usually Python 2 and Python 3 is installed side-by-side. The default Python 3 executable is almost always python3. pip may be available as either of the following, depending on your Linux distribution
pip3
python3-pip
python36-pip
python3.6-pip
Linux
Avoid using pip with sudo! Yes, it's the most convenient way to install Python packages and the executable is available at /usr/local/bin/pipenv, but it also mean that specific package is always visible for all users, and all Python projects that you create. Instead, use per-user site packages instead with --user
pip3 install --user pipenv
pipenv is available at
~/.local/bin/pipenv
macOS
On macOS, Homebrew is the recommended way to install Python. You can easily upgrade Python, install multiple versions of Python and switch between versions using Homebrew.
If you are using Homebrew'ed Python, pip install --user is disabled. The global site-package is located at
/usr/local/lib/python3.y/site-packages
and you can safely install Python packages here. Python 3.y also searches for modules in:
/Library/Python/3.y/site-packages
~/Library/Python/3.y/lib/python/site-packages
Windows
For legacy reasons, Python is installed in C:\Python37. The Python executable is usually named py.exe, and you can run pip with py -m pip.
Global site packages is installed in
C:\Python37\lib\site-packages
Since you don't usually share your Windows devices, it is also OK to install a package globally
py -m pip install pipenv
pipenv is now available at
C:\Python37\Scripts\pipenv.exe
I don't recommend install Python packages in Windows with --user, because the default user site-package directory is in your Windows roaming profile
C:\Users\user\AppData\Roaming\Python\Python37\site-packages
The roaming profile is used in Terminal Services (Remote Desktop, Citrix, etc) and when you log on / off in a corporate environment. Slow login, logoff and reboot in Windows can be caused by a large roaming profile.
OSX GUYS, OVER HERE!!!
As #charlax answered (for me the best one), you can use a more dynamic command to set PATH, buuut for mac users this could not work, sometimes your USER_BASE path got from site is wrong, so you need to find out where your python installation is.
$ which python3
/usr/local/bin/python3.6
you'll get a symlink, then you need to find the source's symlink.
$ ls -la /usr/local/bin/python3.6
lrwxr-xr-x 1 root wheel 71 Mar 14 17:56 /usr/local/bin/python3.6 -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
(this ../../../ means root)
So you found the python path (/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6), then you just need to put in you ~/.bashrc as follows:
export PATH="$PATH:/Library/Frameworks/Python.framework/Versions/3.6/bin"
Installing pipenv globally can have an adverse effect by overwriting the global/system-managed pip installation, thus resulting in import errors when trying to run pip.
You can install pipenv at the user level:
pip install --user pipenv
This should install pipenv at a user-level in /home/username/.local so that it does not conflict with the global version of pip. In my case, that still did not work after running the '--user' switch, so I ran the longer 'fix what I screwed up' command once to restore the system managed environment:
sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
^ found here: Error after upgrading pip: cannot import name 'main'
and then did the following:
mkdir /home/username/.local ... if it doesn't already exist
export PYTHONUSERBASE=/home/username/.local
Make sure the export took effect (bit me once during this process):
echo $PYTHONUSERBASE
Then, I ran the pip install --user pipenv and all was well. I could then run pipenv from the CLI and it did not overwrite the global/system-managed pip module. Of course, this is specific to the user so you want to make sure you install pipenv this way while working as the user you wish to use pipenv.
References:
https://pipenv.readthedocs.io/en/latest/diagnose/#no-module-named-module-name https://pipenv.readthedocs.io/en/latest/install/#pragmatic-installation-of-pipenv https://pip.pypa.io/en/stable/user_guide/#user-installs
I don't know what happened, but the following did the work (under mac os catalina)
$ brew install pipenv
$ brew update pipenv
after doing this i am able to use
$ pipenv install [package_name]
OS : Linux
Pip version : pip3
sudo -H pip3 install -U pipenv
OS : Windows
Pip version : any one
sudo -H pip install -U pipenv
For thse who installed it using sudo pip3 install pipenv, you need to use python3 -m pipenv shell or python3.9 -m pipenv shell
I'm using zsh on my Mac, what worked for me is at first install pipenv
pip3 install --user pipenv
Then I changed the PATH in the ~/.zshrc
vi ~/.zshrc
In the editor press i to insert your text:
export PATH="/Users/yourUser/Library/Python/3.9/bin:$PATH"
Press esc and then write :wq!
Close the terminal and re-open it.
And finally write pipenv
This way worked for me using macOS BigSur 11.1
On Mac you may have to do:
pip3 install pipenv
Then, cd into your root directory to locate the .zshrc file.
Then add this to path
export PATH=/Library/Frameworks/Python.framework/Versions/3.9/bin:$PATH
Note: 3.9 is the version of Python running on your system.
Note: You can access the .zshrc by using cmd + shift + .
in your root directory... the file is hidden by default
Save and restart your terminal
Fixed this easily by installing pipenv with my central package manager (apt)
sudo apt install pipenv
You could easily install pipenv using your package manager (apt, yum, brew) and it adds it directly to your $PATH variables.
More to mention is it works on zsh. I use zsh on Ubuntu and tried adding pipenv to $PATH and other solutions but didn't work till I used apt to install it.
HOW TO MAKE PIPENV A BASIC COMMAND
Pipenv with Python3 needs to be run as "$ python -m pipenv [command]" or "$ python3 -m pipenv [command]"; the "python" command at the beginning varies based on how you activate Python in your shell. To fix and set to "$ pipenv [command]": [example in Git Bash]
$ cd ~
$ code .bash_profile
The first line is necessary as it allows you to access the .bash_profile file. The second line opens .bash_profile in VSCode, so insert your default code editor's command.
At this point you'll want to (in .bash_profile) edit the file, adding this line of code:
alias pipenv='python -m pipenv'
Then save the file and into Git Bash, enter:
$ source .bash_profile
You can then use pipenv as a command anywhere, for example:
$ pipenv shell
Will work.
This method of usage will work for creating commands in Git Bash. For example:
alias python='winpty python.exe'
entered into the .bash_profile and:
$ source .bash_profile
will allow Python to be run as "python".
You're welcome.
On Mac OS X Catalina it appears to follow the Linux path. Using any of:
pip install pipenv
pip3 install pipenv
sudo pip install pipenv
sudo pip3 install pipenv
Essentially installs pipenv here:
/Users/mike/Library/Python/3.7/lib/python/site-packages/pipenv
But its not the executable and so is never found. The only thing that worked for me was
pip install --user pipenv
This seems to result in an __init__.py file in the above directory that has contents to correctly expose the pipenv command.
and everything started working, when all other posted and commented suggestions on this question failed.
The pipenv package certainly seems quite picky.
If you are on MAC
sudo -H pip3 install pipenv
For window users this may be due to conflicting installation with virtualenv. For me it worked when I uninstalled virtualenv and pipenv first, and then install only pipenv.
pip uninstall virtualenv
pip uninstall pipenv
pip install pipenv
Now pipenv install xxx worked for me
After installing pipenv (sudo pip install pipenv), I kept getting the "Command Not Found" error when attempting to run the pipenv shell command.
I finally fixed it with the following code:
pip3 install pipenv
pipenv shell
Here is how I successfully resolved "Pipenv: Command Not Found" on my Mac OSX
You should change the ownership of these directories to your user.
sudo chown -R $(whoami) /usr/local/share
make sure that your user has write permission.
chmod u+w /usr/local/share
Then Consider installing with Homebrew:
brew update
brew install pyenv
This simply solved it for me if you are on windows.
pip install pipenv
Second, replace your <username> in the following paths and add them to the PATH environment variable:
c:\Users\<username>\AppData\Roaming\Python\Python38\Site-Packages
C:\Users\<username>\AppData\Roaming\Python\Python38\Scripts
You need to close the Command Prompt and reopen it.
Third, type the following command to check if the pipenv installed correctly:
pipenv -h
I hope this helps you too!
In this case you just need to add the binary path to your bash. In case you're using ZSH for example you need to edit the.zshrc file as an admind and then add the code mentioned by #charlax on the comments above:
PYTHON_BIN_PATH="$(python3 -m site --user-base)/bin"
PATH="$PATH:$PYTHON_BIN_PATH"
You might consider installing pipenv via pipsi.
curl https://raw.githubusercontent.com/mitsuhiko/pipsi/master/get -pipsi.py | python3
pipsi install pew
pipsi install pipenv
Unfortunately there are some issues with macOS + python3 at the time of writing, see 1, 2. In my case I had to change the bashprompt to #!/Users/einselbst/.local/venvs/pipsi/bin/python
In some cases of old pip version:
sudo easy_install pip
sudo pip install pipenv
First check if pipenv is installed:
pipenv --version
If no version is available on your system, then run the following command to install pipenv
sudo aptitude install pipenv
first install pip using following command
pip3 install pipenv
Now check whether pipenv is showing by using following command
pipenv --version if you see like command not found: pipenv use following commands
Now we have to set the path for pipenv, to do that first we have to find the user base binary directory,
On linux and Mac we can do it as following
python3 -m site --user-base
this command will display something like this
/some_directory/Python/3.9
use the path displayed in your terminal and append /bin at the end, now your path looks like this
/some_directory/Python/3.9/bin
now you have to set the path, if you are using zsh (z shell) type nano ~/.zshrc in the terminal or if you are using code editor like VSCode and path is set for VScode type code ~/.zshrc
if you are using bash use nano ~/.bashrc or code ~/.bashrc
in the file at last add following line
export PATH="$PATH:/somedirectory/Python/3.9/bin"
save the file and exit the terminal
now open new terminal and type pipenv --version you should see something like pipenv, version 2022.10.25
on Windows we can do as following
python -m site --user-site
you should see something like
C:\Users\Username\AppData\Roaming\Python36\site-packages`
now replace site-packages with Scripts.
this could return
C:\Users\Username\AppData\Roaming\Python36\Scripts
You can set your user PATH permanently in the Control Panel. You may need to log out for the PATH changes to take effect.
It's probably installed in your user path.
for instance, if your user(username) is tom check this path
/home/tom/.local/bin/pipenv
if pipenv exists in the path you can move or copy it to the general user path, so you can execute pipenv from all terminal sessions.
cp /home/tom/.local/bin/pipenv /usr/bin/
then you should be able to run pipenv
For me, what worked on Windows was running Command Prompt as administrator and then installing pipenv globally: python -m pip install pipenv.

Python module Installing

I wrote this command to install NLTK python module :
sudo pip install -U nltk
The first time it seemed to work well but when I wanted to test it, it didn't. So I re-wrote the command and then I got
The directory '/Users/apple/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
I tried every chown command possible, I don't know what I can do more. I use a Mac OS X 10.9.5.
You'll want to create a virtualenv to install Python packages. This prevents the need to install them globally on the machine (and generally makes installing modules less painful). We'll also include virtualenvwrapper to make things easier.
The steps are to install virtualenv and virtualenvwrapper with pip:
pip install virtualenv virtualenvwrapper
This may require sudo - if so, just sudo pip install virtualenv virtualenvwrapper.
Add the following lines to your ~/.bashrc:
# Add WORKON_HOME to be the location of all virtual environments
export WORKON_HOME=~/Envs
# Gives us `workon` and `deactivate`
source /usr/local/bin/virtualenvwrapper.sh
Source your ~/.bashrc
. ~/.bashrc
Next, create the virtual environment. I'll generically call this one venv:
mkvirtualenv venv
Now you'll want to do work in that virtual environment. To do this, you'll want to issue workon:
workon venv
Now you can install your packages like normal.
pip install nltk
...
When you're done doing work, just deactivate your virtualenv.
deactivate
Next time you want to do work, just issue workon venv again and all of your modules will still be associated with that virtual environment.

virtualenv does not include pip

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.

Virtualenv Command Not Found

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!

Pip doesn't install packages to activated virtualenv, ignores requirements.txt

I am attempting to setup a development environment on my new dev machine at home. I have just installed Ubuntu and now I am attempting to clone a remote repo from our web-server and install its dependencies so I can begin work.
So far I have manually installed virtualenv and virtualenvwrapper from pypi and edited my bash.rc appropriately to source my virtualenvs when i start my terminal. I then cloned my repo to ~/projects/project-name/websitename.com. Then I used virtualenvwrapper to mkvirtualenv env-name from ~/projects/project-name/websitename.com. This reflects exactly the file-structure/setup of the web-server I am cloning from. So far so good.
I logged into the dev server and activate the virtualenv there and use pip freeze -l > req.txt to render a dependencies list and scp to my local machine. I activate the virtualenv on my local machine, navigate to the ~/projects/project-name/websitename.com and execute pip install -r path-to-req.txt and it runs through all of the dependencies as if nothing is wrong. However, when i attempt to manage.py syncdb i get an error about not finding core django packages. What the hell? So i figure somehow Django failed to install, i run pip install Django==1.5.1 and it completes successfully. I got to setup my site again and get another error about no module named django_extensions. Okay, what the hell with it, i just installed all of these packages with pip?!
So i pip freeze -l > test.txt and cat test.txt, what does it list? Django==1.5.1, the one package I just manually installed. Why isn't pip installing my dependencies from my specified list into my virtualenv? What am I messing up here?
-EDIT-------------
Which pip gives me the path to pip in my virtualenv
I have only 1 virtualenv and it is activated
My usual workflow is to
pip freeze > someFile.txt
and then install with
pip install -r someFile.txt
So I'm certain that this should work just fine. Unfortunately I can't really tell you anything besides make sure to check that
You really are in the virtualenv that you think you are in. Make sure to run
workon yourVirtualEnvName
to activate it just in case that matters.
Make sure to check that pip is within your virtualenv.
which pip
gives me
/path/to/home/.virtualenvs/myVirtEnv/bin/pip
Sorry I can't give you a more concrete answer. I have to do this semi-regularly and I've never had a problem with it skipping dependencies. Best of luck!
Struggled with some variation of this issue not long ago; it ended up being my cluttered .bash_profile file.
Make sure you don't have anything that might mess up your virtualenv inside your .bash_profile/.bashrc, such as $VIRTUAL_ENV or $PYTHONHOME or $PYTHONPATH environment variables.
I know this is an old post, but I just encountered a similar problem. In my case the cause was that I was running the pip install command using sudo. This made the command run globally and the packages install in the global python path.
Hope that helps somebody.

Categories