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

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.

Related

How to correct wrong pip path in venv?

I have a Flask app that with and using venv for my virtual environment. For some reason, which pip has suddenly stopped installing packages in venv/lib/python3.9/site-packages, but to a completely different repository on my system. How do I redirect pip to install packages to the correct path in venv?
instead of using pip as a stand alone command in terminal, i suggest you to use python -m pip instead, here python will be assoicated with the project (in case of Virtual environment) or whole system level (in case docker single app ). This will keep track and tell the system to run all package/module related to python interpreter assoicated with the project only
You can use the -t flag provided by the pip install command.
pip install <package_name> -t <full_location_path>

Virtualenv installing a package in the global directory

I made sure to read this question and similar ones, but I couldn't find an answer to my problems.
My problem is: when I head into muy virtual env and activate it, if I install a package there, it is also installed elsewhere in my computer.
So, for example, if I type in the terminal:
cd home/Documents/Python/tests/my_virtual_env
source bin/activate
That activates the virtual environment. If I type:
pip3 install wget #just an example package
I see the installation process and I can run a .py script that uses wget. However, why is this package also installed elsewhere in my computer?
I made sure I hadn't that package installed beforehand using pip3 list.
I confirmed that package was installed elsewhere by running a .py script from other directories (using cd /etc.etc/ to change directory and then running it from there).
I deactivated the environment in the right moment.
I also realized that if I uninstall that package within the virtualenv, it is also uninstalled elsewhere.
Thank you so much for your help.
It could be that the pip3 command being executed is not actually tied to the virtual environment. So instead you could the following, which would work whether or not the virtual environment is activated:
$ path/to/my_virtual_env/bin/python3 -m pip install SomeProject
The following command should give you a relatively clear indication of where exactly the project has been installed, make sure it is in the site-packages directory of the virtual environment:
$ path/to/my_virtual_env/bin/python3 -m pip show SomeProject
So it should show something of the sort:
Name: SomeProject
...
Location: .../path/to/my_virtual_env/lib.python3.X/site-packages
However, why is this package also installed elsewhere in my computer?
The following shows where a binary is located:
$ which somecommand
It should be relatively easy to recognize if somecommand is in a Python virtual environment or not.

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

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.

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.

Have MySQLdb installed, works outside of virtualenv but inside it doesn't exist. How to resolve?

I'm using the most recent versions of all software (Django, Python, virtualenv, MySQLdb) and I can't get this to work. When I run "import MySQLdb" in the python prompt from outside of the virtualenv, it works, inside it says "ImportError: No module named MySQLdb".
I'm trying to learn Python and Linux web development. I know that it's easiest to use SQLLite, but I want to learn how to develop larger-scale applications comparable to what I can do in .NET. I've read every blog post on Google and every post here on StackOverflow and they all suggest that I run "sudo pip install mysql-python" but it just says "Requirement already satisfied: mysql-python in /usr/lib/pymodules/python2.7"
Any help would be appreciated! I'm stuck over here and don't want to throw in the towel and just go back to doing this on Microsoft technologies because I can't even get a basic dev environment up and running.
If you have created the virtualenv with the --no-site-packages switch (the default), then system-wide installed additions such as MySQLdb are not included in the virtual environment packages.
You need to install MySQLdb with the pip command installed with the virtualenv. Either activate the virtualenv with the bin/activate script, or use bin/pip from within the virtualenv to install the MySQLdb library locally as well.
Alternatively, create a new virtualenv with system site-packages included by using the --system-site-package switch.
source $ENV_PATH/bin/activate
pip uninstall MySQL-python
pip install MySQL-python
this worked for me.
I went through same problem, but using pip from virtualenv didn't solve the problem as I got this error
error: could not delete '/Library/Python/2.7/site-packages/_mysql.so': Permission denied
Earlier I had installed the package by sudo pip install mysql-python
To solve, copy files /Library/Python/2.7/site-packages/MySQL_python-1.2.5-py2.7.egg-info and /Library/Python/2.7/site-packages/_mysql* to ~/v/lib/python-2.7/site-packages and include /usr/local/mysql/lib in DYLD_LIBRARY_PATH env variable.
For the second step I am doing export DYLD_LIBRARY_PATH=/usr/local/mysql/lib in ~/.profile

Categories