pip has to reinstall all packages in exported virtualenv - python

I have a question about python virtualenv. I get a virtualenv for a project with all packages required to run that project. But when i run it for the first time and it crash 'cause python has some requirements not satisfied. So i check if there is all packages inside:
virtualenv/lib/python2.7/site-packages/
and all packages required are inside.
But when i type:
pip list
packages doesn't shown. So i have to run:
pip install -r requirements.txt
pip downloads them again.
So my question is, why pip downloads and reinstall them again if they are installed yet ? And how i can force pip to reinstall all packages inside virtualenv ?

The problem was that all scripts inside the virtualenv were created on another pc with them paths. Indeed when i launched python or pip from virtualenv they ran from my global path 'cause couldn't find virtualenv script path and in particular pip shown my global packages.
Fixing directives path of all script inside virtualenb/bin/ to my real virtualenv path solved this issue.

Related

Old pip packages still exist after uninstalling python - how do I fix this?

I have just uninstalled some older python versions (3.9x, 3.10x) on windows 10 and reinstalled the latest python 3.10.7. My environment variables all reference the correct location for python, but when I run pip list I get a bunch of old packages when they should no longer be there. Since I uninstalled python with the intention to have a fresh installation, I don't want those old packages, but they are even still importable. If I run pip -v list, I see most of those (except the base packages that come with python) refer to ../appdata/roaming/python/python310/, instead of ..appdata/local/..., where Python is actually installed.
What can I do to clear this up? Sure I could pip uninstall them, but I don't know if this indicates a deeper issue and I am quite confused by it.
You get a ton of old packages because you didn't work with venvs sometimes! You can safely run this command to uninstall them all pip uninstall -y -r <(pip freeze) or either you can use your requirements.txt file to uninstall only specified libraries, as suggested in this thread.
Working with venvs fixes this exact problem. Every project will have its own environment with its own libraries to be installed. It may sound heavier (for example, if you have three projects that need requests, you will have to install requests three times) but you won't have any sort of version incompatibility since you can safely install different versions of the same package!
Always remember to set up the venv either using PyCharm or running these commands once you are in your folder:
python -m venv .
source venv/bin/activate
pip install -r requirements.txt
Either, if you will use Linux/Ubuntu, you can do apt install virtualenv, which also automatically creates a venv using this command:
virtualenv venv
Using venv also helps using a different Python version for each project meanwhile having them all installed! Here is how to change the version.
To exit a venv, just run the command deactivate!
To understand if you're in a venv, you have to check whether (venv) is written before your host in the cmd or in the PyCharm terminal, like so:
NOT IN VENV:
marco#marco-Aspire-A515-51G:~
IN VENV:
(venv) marco#marco-Aspire-A515-51G:~

Django pip freeze > requirements.txt not getting the exact packages installed in the virtual env

Django pip freeze > requirements.txt not getting the exact packages installed in the virtual env rather it's getting all the packages i have ever installed and it's kinda not what i exactly wants, let me show some image of whats happening
there are still more packages below, please what can i do
Whenever you do
pip freeze
It prints out all the installed packages. May be you are confusing with packages installed as dependency of manually installed packages.
For example if you install Fastapi, it will also install jinja2
I can't think of any case where you want packages installed by you and not their dependencies. Its not a problem at all.
On the other hand if its actually giving you all the packages ever installed, it means you have installed all your packages in the same environment always. You should use different environments for each of your project (sometimes even more than one for a single project). In this case, create another virtual environment, install requirements and then again use pip freeze. Steps below.
python3 -m venv venv
source venv/bin/activate
pip install {required packages}
pip freeze > requirements.txt

Pip won't install packages in virtualenv

After performing a system upgrade this morning, I'm not able to install or update packages in any of my old or fresh created virtual environments.
I create the virtual environments using virtualenvwrapper.
I've tried rebooting my PC, recreating all of my virtual environments, but nothing worked.
Strange enough, pip freeze does not output my installed packages, although they are found in sitepackages.
Im using python3.7 on manjaro linux with the newest kernel.
In my envs and on my system pip 19.2.3 is installed.
The error I'm seeing is the following:
pip install <package>
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
When trying to use the system pip without virtualenv, everything works just fine.
pip install cython
Requirement already satisfied: cython in /home/manuel/.local/lib/python3.7/site-packages (0.29.13
I expect pip to install the desired package to the sitepackages folder of my virtualenv without telling me to not use a '--user' install, which I'm not doing explicitly anyway
I was able to fix this myself.
I checked all the pip config files here https://pip.pypa.io/en/latest/user_guide/#configuration
Seems like in the pip configuration file /etc/pip.conf the user parameter was set to true.
Changing it to false resolved my issue.
I still don't know why/if the config file changed.

How can I see what packages were installed using `sudo pip install`?

I know that installing python packages using sudo pip install is bad a security risk. Unfortunately, I found this out after installing quite a few packages using sudo.
Is there a way to find out what python packages I installed using sudo pip install? The end goal being uninstallment and correctly re-installing them within a virtual environment.
I tried pip list to get information about the packages, but it only gave me their version. pip show <package name> gave me more information about an individual package such as where it is installed, but I don't know how to make use of that information.
any modules you installed with sudo will be owned by root, so you can open your shell/terminal, cd to site-packages directory & check the directories owner with ls -la, then any that has root in the owner column is the one you want to uninstall.
When you run sudo pip install, pip will install the package in your global site-packages directory.
So, to determine which packages you've installed with sudo pip install, you can navigate to your /site-packages directory.
The site-packages directory is a sub-directory of your python installation. For example, /Users/me/Library/Python/2.7/lib/python/site-packages.
This SO post has a more detailed discussion regarding how to find the site-packages directory.
Hope this helped!
try the following command: pip freeze

Multiple python installations and pip, dude, where is my site-packages?

As we all know, Mac OS ships with its own python pre installed.
The recommendation seems to be to leave that alone and use homebrew to install a fresh python into the system.
My issue is that after installing python (and pip) using homebrew, pip is installing packages into the Mac OS site-packages instead of my own. I have confirmed I am running the "homebrew" pip:
$ which pip
/usr/local/bin/pip
But then when I pip install something I can se it is installed at:
/lib/python2.7/site-packages
Pip should be installing at /usr/local/lib/python2.7/site-packages unless i'm miss understanding something.
The surprising thing is that checking with -V yields a surprising result:
pip -V
pip 7.1.0 from /usr/local/lib/python2.7/site-packages (python 2.7)
Running pip list just after running pip install does not show the packages that were supposedly just installed by it but went to the wrong site-packages.
Adding to this, the packages installed on the /lib/python2.7/site-packages are not recognized by my $PYTHONPATH and as such I cannot use them.
To add even more confusion, I decided to use a virtualenv, but I was amazed as even using pip with the virtualenv active kept installing to the /lib/python2.7/site-packages instead of the virtualenv site-packages.
So, somehow I ended up with a homebrew pip, that installs packages outside of the homebrew site-packages and a python interpreter that cant use the packages installed by pip.
How do you recommend I go about finding the root cause and having a smooth python experience? :)
You can easily find you site-packages directory by invoking this command
python -c 'import site; print(site.getsitepackages())'
I think after you activate a virtualenv your python path should point to that environments site-package location--if not it's probably not activated. Only once you activate it will you run pip so it installs in that virtual env's site-packages. if it's not activated, it will go in whatever other site-packages it already knows about:
Step 1: create a virtual env
a la... virtualenv venv
Do this only once!
Step 2: Activate the vitual env
something like source /venv/bin/activate
Needs doing every time you want to use this virtual environment
Step 3: run pip commands, watch them get installed in the virtual env site-packages!
If you do step 3 before step 2 your not actually using the virtual environment you created, so all bets are off--That's probably the reason pip is still installing to the old location.
Now, my overall recommendation is to go further and use pyenv to install specific version of python into your /Users/username/.pyenv folder and abandon both the default OSX and homebrew packages. It's simple and you can control easily the exact version of python to use by simple issuing of command to change versions.
THEN use virtualenv in python2 or pyvenv if in python3 (not to be confused with pyenv) to build vitual environments with their own local site-packages to store pip modules. When you activate a virtualenv, your $PYTHONPATH will switch to the specific location.
The flow would then be:
Use pyenv to pull down and switch to a specific version of python you want to use--overriding homebrew and the OSX version.
Create your vitrualenv. This will create a bin that will link to the pyenv python stack you just specified in the previous step.
Activate the virtual env, and proceed.
Totally control your environment!
For one, you could try updating pip with pip install --upgrade pip command, which might or might not redirect your pip path.
Two, and I should have really started with this one is to set the preferred pip executable path in the .bash_profile or .zshrc if you are using one. The way you do it (on Mac) is by holding Shift+Command+Period to reveal hidden files, going to the User folder and opening the .bash_profile/.zshrc with a text editor. Afterward, add the path/to/bin where the pip that you require is. Like export PATH="User/Username/anaconda3/bin:$PATH" or /usr/local/bin or path/to/venv/bin. Whatever code you write in the end will overwrite the previous one.
Three, if you don't wanna change your default pip, but rather wanna use a different version for that specific case just include the full path of the pip executable like /usr/local/bin/pip list or Users/Username/Desktop/venv/bin/pip install module.

Categories

Resources