Python - Pip install requirements only if dependencies are not satisifed - python

I am wondering about this command pip install -r requirements.txt. Does pip install modules if they are not satisfied or does it try and install anyway even if the modules are already there? If it is the latter, than is there any way to write a shell script which checks if dependencies are satisfied and if not invoke pip install?

Pip only installs packages that are not installed yet.
This does mean that even if a new version is available, old packages will be kept. You can pass the --upgrade flag to prevent that behavior and install the latest versions (but then pip will call pypi for every package in your requirements file, in order to identify its latest version).
An alternative is to have version specifiers in your requirements file (e.g. mypackage==1.2.3), so that if you change your requirements file and use new versions, pip will pick those up without the --upgrade flag.

Related

How to ignore my development project for installing a package with pip

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

Removing All Packages With Defunct Dependees in PIP

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.

Install packages using pip for updated versions of python

I recently updated from Python 3.5 to Python 3.6 and am trying to use packages that I had previously downloaded, but they are not working for the updated version of Python. When I try to use pip, I use the command "pip install selenium" and get the message "Requirement already satisfied: selenium in /Users/Jeff/anaconda/lib/python3.5/site-packages" How do I add packages to the new version of Python?
First, make sure that your packages do have compatibility with the version of Python you're looking to use.
Next, run pip freeze > requirements.txt in the base directory of your Python project. This puts everything in a readable file to re-install from. If you know of any packages that require a certain version that you'll want to re-install, put package==x.x.x (where package is the package name and x.x.x is the version number) in the list of packages to make sure it downloads the correct version.
Run pip uninstall -r requirements.txt -y to uninstall all packages. Afterwards, run pip install -r requirements.txt.
This allows you to keep packages at the correct version for the ones you assign a version number in requirements.txt, while upgrading all others.

Manually set package as installed in Python/pip

I'm installing the openbabel package, and it can automatically generate the necessary Python libraries during compilation. This saves a good chunk of time, since installing from source via pip takes a few minutes, and that time can be rolled into the initial compilation.
I've listed it as a requirement in my requirements.txt file, but when I go to install (pip install -r requirements.txt), it attempts to reinstall the openbabel Python library. When I run pip show or pip list, openbabel doesn't show up.
Is there a way to manually mark a package as installed so pip thinks it's installed, even if it can't find the package? Or is there a file I can create that pip will use that will tell it openbabel is installed?
Create an empty .egg-info file in your site-packages directory.
For example, on my machine I did touch /usr/lib64/python3.6/site-packages/GLWindow-1.8.0-py3.6.egg-info to trick pip3 into thinking that I've installed GLWindow.

pip install private package

I have one python package A which has depends on another private package named godot(hosted at bitbucket, and should be accessed by git+ssh protocol). In package A's setup.py, I have following code:
...
install_requires=['godot'],
dependency_links=['git+ssh://git#bitbucket.org/xxx/godot.git#egg=godot']
...
I have two questions here:
Now setuptools 1.4 (latest stable version) does not support 'git+ssh' protocol, only code in the development branch handle this protocol: Python setuptools: How can I list a private repository under install_requires?. I have installed the development version via:
pip install --upgrade --force-reinstall hg+https://bitbucket.org/pypa/setuptools#egg=setuptools
I almost solved this bit, but I wonder If any other approach available? Invoke pip install -r requirements.txt(have git+ssh://git#bitbucket.org/xxx/godot.git#egg=godot list in requirements.txt)?
The second question is name conflict. There is another package on pypi also named godot, So when I install package A using follow command, pip install the godot from pypi index:
pip install git+ssh://git#pypi.corp.com/xxx/A.git#egg=A
How could force pip(setup.py) to install the private godot package, rather than the one on pypi index?
For part 1: you can install packages via pip by specifying as:
$ pip install http://my.package.repo/SomePackage-1.0.4.zip
To keep it simple and avoid spending undue time on it, I would just download the .zip source file and install via pip as above.
See here...
For part 2: pip has a --no-dependencies switch. Add that after installing all the dependencies manually

Categories