I have a repository with an inflated requirements.txt file that I'd like to clean up. Using pipreqs I've set my requirements.txt to be a minimal set of packages need for my repository. To test this, I setup a virtualenv to install the packages and then run all my unit tests to make sure they're satisfactory.
virtualenv temp_venv --no-site-packages
source temp_venv/bin/activate
pip install -r requirements.txt
Which runs fine, but I see that a whole bunch of extra packages are collected and installed. Why? Are these identified as needed by required packages, and thus installed? If so, should I then include them in the requirements.txt?
Yes. The packages are dependencies of your dependencies​.
But no, you should not specify them directly. Automatic tools know to download dependencies recursively and it would significantly add to maintenance overhead.
This might have been because of the dependencies of your written libs in requirements.txt. For ex: if you have written scipy as requirement numpy will also be installed because scipy is dependent on numpy.
Well, for me the above answers were not the case. Pip install was installing extra packages not in requirements.txt . The solution was:
Run conda create -n venv_name and conda activate venv_name, where venv_name is the name of your virtual environment.
Run conda install pip. This will install pip to your venv directory.
Then run pip install -r requeriments.txt
The above answer was adapted from here: Using Pip to install packages to Anaconda Environment
I uploaded my package to testpypi, and installed it via:
pip install -i https://test.pypi.org/simple/ myporj==0.1.6
However it refuse to install it by saying:
Requirement already satisfied: myproj==0.1.6 in ./projs/myproj (0.1.6)
I guess I may add the project in editable mode:
pip install --editable .
However, I know want to disable it. I tried:
python setup.py develop --uninstall
But it has no effect.
It may be worth creating a separate env (Virtual Environments) for the installation.
Here are some articles on this subject:
https://docs.python.org/3/tutorial/venv.html
https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment
Or does it need to be installed in the same place?
You can try to find your package pip search myporj or pip list for show all packages.
And uninstall it later pip uninstall myporj (it may require the right of sudo in linux) then install again.
Maybe you may need --no-cache-dir option to ignore the cache during installation. Here is more details: https://pip.pypa.io/en/stable/reference/pip_install/#caching
I am wondering if there is a way to uninstall packages in PIP including those that are not listed in the requirements.txt but which were installed as dependencies of those that are.
For example, suppose I have Django==2.1 line in requirements.txt. When running pip install -r requirements.txt, the above will instruct PIP to install many extra packages on which Django depends.
However, if I then execute pip uninstall -r requirements.txt, the Django package will be uninstalled, but PIP will retain many of its now unused dependencies.
My question is how to go about cleaning those up nicely. Is there a way to make PIP preserve and consider history explicitly? If the thing which forced PIP to install a package is being uninstalled, it appears that we should also be able to flag it to wipe its now defunct dependencies.
Take a look at pipdeptree Python package and pipdeptree --reverse some_package command in particular.
The easiest option is to use pip-autoremove. After installing it via pip, you can simply call the following from the command line:
pip-autoremove Django
Which uninstalls Django and its unused dependencies including those not listed in requirements.txt.
Is there a way to install packages with pip to avoid the need to repeatedly delete files like:
pip can't proceed with requirement 'Flask-Restless==0.13.1 (from -r requirements.txt (line 2))' due to a pre-existing build directory.
location: /private/var/folders/0k/t9lwmd2j1212pxydpr6l596h0000gq/T/pip_build_jacob/Flask-Restless
This is likely due to a previous installation that failed.
pip is being responsible and not assuming it can delete this.
I'm on round 4 of doing this and have no idea how long it may take to get through.
Looking at pip --help isn't helpful and man pip returns nothing.
As it has already been mentioned it's better to use virtualenv in order to avoid python package chaos on your system and install the python packages only for particular projects.
However, in your particular case you can try the following in the terminal:
pip uninstall flask-restless
Then try to run the installation again:
pip install -r requirements.txt
The options to consider during installation:
--force-reinstall
--ignore-installed
--no-deps
Add these options to the end of pip install -r requirements.txt to play with them and see if they can help.
Using
--force-reinstall
may solve your issue.
I would also recommend considering using a virtualenv for each project you are working on.
https://virtualenv.pypa.io/en/stable/
You can then activate the virtual environment for that project and pip
pip install -r requrements.txt
will install dependencies for that project in the virtual environment instead of globally. This will reduce the odds of having weird conflicts like you are having and if you do have an issue you can blow away the virtualenv and reinstall just the dependencies for that project without borking your global packages.
I'm running Ubuntu 9:10 and a package called M2Crypto is installed (version is 0.19.1). I need to download, build and install the latest version of the M2Crypto package (0.20.2).
The 0.19.1 package has files in a number of locations including (/usr/share/pyshared and /usr/lib/pymodules.python2.6).
How can I completely uninstall version 0.19.1 from my system before installing 0.20.2?
The best way I've found is to run this command from terminal
sudo pip install [package_name] --upgrade
sudo will ask to enter your root password to confirm the action.
Note: Some users may have pip3 installed instead. In that case, use
sudo pip3 install [package_name] --upgrade
You might want to look into a Python package manager like pip. If you don't want to use a Python package manager, you should be able to download M2Crypto and build/compile/install over the old installation.
To automatically upgrade all the outdated packages (that were installed using pip), just run the script bellow,
pip install $(pip list --outdated | awk '{ print $1 }') --upgrade
Here, pip list --outdated will list all the out dated packages and then we pipe it to awk, so it will print only the names.
Then, the $(...) will make it a variable and then, everything is done auto matically. Make sure you have the permissions. (Just put sudo before pip if you're confused)
I would write a script named, pip-upgrade
The code is bellow,
#!/bin/bash
sudo pip install $(pip list --outdated | awk '{ print $1 }') --upgrade
Then use the following lines of script to prepare it:
sudo chmod +x pip-upgrade
sudo cp pip-upgrade /usr/bin/
Then, just hit pip-upgrade and voila!
Via windows command prompt, run: pip list --outdated
You will get the list of outdated packages.
Run: pip install [package] --upgrade
It will upgrade the [package] and uninstall the previous version.
To update pip:
py -m pip install --upgrade pip
Again, this will uninstall the previous version of pip and will install the latest version of pip.
Method 1: Upgrade manually one by one
pip install package_name -U
Method 2: Upgrade all at once (high chance rollback if some package fail to upgrade
pip install $(pip list --outdated --format=columns |tail -n +3|cut -d" " -f1) --upgrade
Method 3: Upgrade one by one using loop
for i in $(pip list --outdated --format=columns |tail -n +3|cut -d" " -f1); do pip install $i --upgrade; done
I think the best one-liner is:
pip install --upgrade <package>==<version>
Open Command prompt or terminal and use below syntax
pip install --upgrade [package]==[specific version or latest version]
For Example
pip install --upgrade numpy==1.19.1
How was the package originally installed? If it was via apt, you could just be able to do apt-get remove python-m2crypto
If you installed it via easy_install, I'm pretty sure the only way is to just trash the files under lib, shared, etc..
My recommendation in the future? Use something like pip to install your packages. Furthermore, you could look up into something called virtualenv so your packages are stored on a per-environment basis, rather than solely on root.
With pip, it's pretty easy:
pip install m2crypto
But you can also install from git, svn, etc repos with the right address. This is all explained in the pip documentation
pip install -U $(pip list --outdated | awk 'NR>2 {print $1}')
In Juptyer notebook, a very simple way is
!pip install <package_name> --upgrade
So, you just need to replace with the actual package name.
How can I completely uninstall version 0.19.1 from my system before
installing 0.20.2?
In order to uninstall M2Crypto use
pip uninstall M2Crypto
I need to download, build and install the latest version of the
M2Crypto package (0.20.2).
In order to install the latest version, one can use PyPi
pip install M2Crypto
To install the version 20.2 (an outdated one), run
pip install M2Crypto==0.20.2
Assuming one just wants to upgrade
pip install M2Crypto --upgrade # Or pip install M2Crypto -U
Notes:
Depending on one's Python version (here's how to find the version) one may use a different pip command. Let's say one is working with Python 3.7, instead of just using pip, one might use pip3.7.
Using sudo is considered unsafe.
Nowadays there are better practices to manage the development system, such as: virtual environments or development containers. The development containers allow one to put the entire development environment (be it modules, VS Code extensions, npm libraries,...) inside a Docker container. When the project comes to an end, one closes the container. There's no need to keep all of those requirements around in the computer for no reason. If you feel like reading more about it: Visual Studio Docs, Github.
Get all the outdated packages and create a batch file with the following
commands
pip install xxx --upgrade for each outdated packages
I.e.:
python -m pip install --proxy <proxyserver_name>:<port#> <pkg_name>
Remember to export the variables after setting them, to make them available to the outer shell session.
Windows:
Add to environment variables:
set HTTP_PROXY=<proxyserver_name>:<port#>
You might have to install the full python package first