I am trying to show python libraries in requirements.txt by using "pip freeze > requirements.txt" and my virtual environment is still activated.
But instead of getting my current virtual env's libraries, I am getting all the libraries in my requirements.txt file that i used in other environment earlier.
What is the issue?
Your virtualenv is certainly messed up.
To find the packages which are not required, run:
pip list --not-required
But, I think it's time to recreate your virtualenv.
Related
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 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
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.
How do you check what package and version are installed inside virtualenv?
My thought was to create requirement.txt file. But, is there another way to do this from CLI?
Once you activate your virtual environment, you should be able to list out packages using pip list and verify version using python --version.
pip list will show you all the packages that are installed for the virtualenv. I see you are wanting to create the requirements.txt file from a CLI, so you can run this command
pip freeze > requirements.txt
This will give you a requirements.txt file inside your current directory, for ALL the packages and libraries that are installed for that virtualenv.
Personally, I like using pigar. All you need to do after installing it is run pigar and it will search through your Python files inside the current directory and create a requirements.txt file for you based on all your imports from those files.
I am using a virtualenv. I have fabric installed, with pip. But a pip freeze does not give any hint about that. The package is there, in my virtualenv, but pip is silent about it. Why could that be? Any way to debug this?
I just tried this myself:
create a virtualenv in to the "env" directory:
$virtualenv2.7 --distribute env
New python executable in env/bin/python
Installing distribute....done.
Installing pip................done.
next, activate the virtual environment:
$source env/bin/activate
the prompt changed. now install fabric:
(env)$pip install fabric
Downloading/unpacking fabric
Downloading Fabric-1.6.1.tar.gz (216Kb): 216Kb downloaded
Running setup.py egg_info for package fabric
...
Successfully installed fabric paramiko pycrypto
Cleaning up...
And pip freeze shows the correct result:
(env)$pip freeze
Fabric==1.6.1
distribute==0.6.27
paramiko==1.10.1
pycrypto==2.6
wsgiref==0.1.2
Maybe you forgot to activate the virtual environment? On a *nix console type which pip to find out.
You can try using the --all flag, like this:
pip freeze --all > requirements.txt
Although your problem was specifically due to a typo, to help other users:
pip freeze doesn't show the dependencies that pip depends on. If you want to obtain all packages you can use pip freeze --all or pip list.
If you have redirected all the pre-installed packages in a file named pip-requirements.txt then it is pretty simple to fix the above issue.
1) Delete your virtualenv folder or create new one (I am giving it a name as venv)
rm -rf venv && virtualenv venv
2) Install all the requirements/dependencies from the pip-requirements.txt
pip install -r pip-requirements.txt
3) Now you can check the installed packages for your Django application
pip freeze
4) If you had forgotten to update your requirements file(pip-requirements.txt), then install fabric again (Optional Step)
Note: After installing any dependency for your Django app, always update the requirements in any file as follows (make sure your virtualenv is activated)
pip freeze > pip requirements.txt
That's it.
Adding my fix in addition of above fix also ,
I was also facing the same issue on windows,even after activating the virtualenv too pip freeze was not giving me all list of installed packages. So i upgraded my pip with python -m pip install --upgrade pip command and then used pip freeze.
This time it worked and gave me all list of installed packages.
This might be stupid but I have got the same problem. I solved it by refreshing vs code file directory (inside vscode there is a reload button). :)
If none of the above answers are working for you.
As with me you might have problem in you venv and pip configuration.
Go inside your venv/bin and open pip and see the 2nd line as:
'''exec' "path/to/yourvenv/bin/python3" "$0" "$#"
See if this line is correctly pointing inside your venv or not
For example in my case.
I initially named my virtual environment as venv1
and later just renamed it to venv2.
In doing so my pip file 2nd line had: '''exec' "venv1/bin/python3" "$0" "$#"
which to work properly should have: '''exec' "venv2/bin/python3" "$0" "$#" notice "venv2" not "venv1" since venv1 in now renamed to venv2.
Due to this python was looking inside pip of venv2 and throwing error or not working as desired.
For those who added Python modules via PyCharm IDE, after generating a virtual environment from the command prompt, good luck! You will need to rebuild the requirements.txt file manually with the ones missing by first running pip3 freeze and adding what is missing from PyCharm.
I highly suggest switching to Visual Studio Code.