Checking if a package is installed specifically via pip - python

Checking if a particular package is available from within Python can be done via
try:
import requests
except ImportError:
available = False
else:
available = True
Additionally, I would like to know if the respective package has been installed with pip (and can hence been updated with pip install -U package_name).
Any hints?

I believe one way to figure out if a project has been installed by pip is by looking at the content of the INSTALLER text file in the distribution's dist-info directory for this project. With pkg_resources from setuptools this can be done programmatically like the following (error checking omitted):
import pkg_resources
pkg_resources.get_distribution('requests').get_metadata('INSTALLER')
This would return pip\n, in case requests was indeed installed by pip.

You said subprocess.call() is allowed, so
available = not(subprocess.call(["pip", "show", "requests"]))

Related

pip install local package missed new functions

I have a Python library named imgtoolkit which I installed locally before. It provides 2 major functions: find_duplicate() and find_blur(). These functions work perfectly. Previously I used pip install . in the package root folder, where setup.py is located.
Today I added one more function analyze_blur() to the code. I uninstalled the local-installed package via pip uninstall imgtoolkit and re-install it via pip install ..
However, the new function cannot be found.
AttributeError: module 'imgtoolkit.tools' has no attribute 'analyze_blur'
The test script looks like this:
from imgtoolkit import tools
if __name__ == '__main__':
tools.analyze_blur() # raises AttributeError
tools.find_blur() # works fine
What did I miss?
I also tried to update the release on PyPI (from v0.0.4 to v0.0.5), but the same problem exists. PyPI project page: https://pypi.org/project/imgtoolkit/
And the related source is at: https://github.com/shivanraptor/imgtoolkit/blob/master/imgtoolkit/tools.py (line 48 & 74)

How to get the path where pip installs `data_files`?

Running pip install seems to create the directory structure + files specified in data_files in /usr/local
However, if I run:
import sys
sys.prefix
I get the string /usr.
Is there any way to figure out where pip installed the data_files for a specific package in a distribution/OS agnostic way ?
Note: I am installing a package from a github repostiroy instead of pypi so maybe this results in the different behavior ?
I believe you should work with sysconfig.
First try:
path/to/pythonX.Y -m sysconfig
And then try its get_path function:
import sysconfig
data_path_str = sysconfig.get_path('data')
print("data_path_str", data_path_str)

No module named 'bankdate'(How to import just .py??)

Sorry that I have no idea how to describe this situation. The bigger package I like to install is "finance" (http://pydoc.net/finance/0.2502/finance.bankdate/). I downloaded it and unzipped to install using python setup.py install.
However, I cannot resolve importing another sub-module
bankdate(.py)
When I use finance module, there comes the error message, "ImportError: No module named 'bankdate'.(It is required in "__init__.py" under finance.) bankdate.py seems to be file under finance folder. How could I install "bankdate"?? Does anybody help me with this??
Thank you~!
cf) pip install bankdate, easy_install bankdate don't work in this case.
I do not know if you are working with Linux or Windows. But it would be a good start checking if the package was installed properly. You could use the following code to check installed packages and its versions:
import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)
By doing so packages installed using both setuptools and pip will appear in a list below. If your package does not appear in the list there you have, it was not installed.
Neverhteless try to import the module usign this:
from finance import bankdate
And see if the error continue. Hope it helps.
you can use pip install finance or you can download .whl file use pip intsall .whl
you can try

How should I handle importing third-party libraries within my setup.py script?

I'm developing a Python application and in the process of branching off a release. I've got a PyPI server set up on a company server and I've copied a source distribution of my package onto it.
I checked that the package was being hosted on the server and then tried installing it on my local development machine. I ended up with this output:
$ pip3 install --trusted-host 172.16.1.92 -i http://172.16.1.92:5001/simple/ <my-package>
Collecting <my-package>
Downloading http://172.16.1.92:5001/packages/<my-package>-0.2.0.zip
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\<me>\AppData\Local\Temp\pip-build-ubb3jkpr\<my-package>\setup.py", line 9, in <module>
import appdirs
ModuleNotFoundError: No module named 'appdirs'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\<me>\AppData\Local\Temp\pip-build-ubb3jkpr\<my-package>\
The reason is that I'm trying to import a third-party library appdirs in my setup.py, which is necessary for me to compute the data_files argument to setup():
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
import os
from collections import defaultdict
import appdirs
from <my-package>.version import __version__ as <my-package>_version
APP_NAME = '<my-app>'
APP_AUTHOR = '<company>'
SYSTEM_COMPONENT_PLUGIN_DIR = os.path.join(appdirs.user_data_dir(APP_NAME, APP_AUTHOR), 'components')
# ...
setup(
# ...
data_files=component_files,
)
However, I don't have appdirs installed on my local dev machine and I don't expect the end users to have it either.
Is it acceptable to rely on third-party libraries like this in setup.py, and if so what is the recommended approach to using them? Is there a way I can ensure appdirs gets installed before it's imported in setup.py, or should I just document that appdirs is a required package to install my package?
I'm ignoring licensing issues in this answer. You definetly need to take these into account before you really do a release.
Is it acceptable to rely on third-party libraries like this in setup.py
Yes, it is acceptable but generally these should be minimized, especially if these are modules which have no obvious use for the end-user. Noone likes to have packages they don't need or use.
what is the recommended approach to using them?
There are basically 3 options:
Bootstrap them (for example use pip to programmatically install packages). For example setuptools provides an ez_setup.py file that can be used to bootstrap setuptools. Maybe that can be customized to download and install appdirs.
Include them (especially if it's a small package) in your project. For example appdirs is basically just a single file module. Pretty easy to copy and maintain in your project. Be very careful with licensing issues when you do that!
Fail gracefully when it's not possible to import them and let the user install them. For example:
try:
import appdirs
except ImportError:
raise ImportError('this package requires "appdirs" to be installed. '
'Install it first: "pip install appdirs".')
You could use pip to install the package programmatically if the import fails:
try:
import appdirs
except ImportError:
import pip
pip.main(['install', 'appdirs'])
import appdirs
In some circumstances you may need to use importlib or __import__ to import the package after pip.main or referesh the PATH variable. It could also be worthwhile to include a verification if the user really wants to install that package before installing it.
I used a lot of the examples from "Installing python module within code" and I haven't personally tried used this in setup.py files but it looks like it could be a solution for your question.
You can mention install_requires with the dependencies list. Please check the python packaging guide here. Also you can provide a requirements.txt file so that it can be run at once using "pip install -r"

getting python module complete version with pkg_resources

I made a python package using distutils that in its setup.py file, has:
setup(name = "foo",
version = "0.2.1",
...)
when I do:
import pkg_resources
pkg_resources.get_distribution("foo").version
I get 0.2 and not 0.2.1. Why is that? how can i get the full version? thank you.
pkg_resources looks for installed distributions in your Python installation. Have you re-ran python setup.py install or python setup.py develop after you’ve changed the version?
Try inspected the object returned by get_distribution for an attribute showing where the location is located on the file system; maybe foo is not installed where you think it is, and an older version is found instead.
It looks like a bug to me. If the package was installed with distutils instead of setuptools, then pkg_resources.get_distribution() returns the oldest version installed.
The best way to fix it is to replace:
from distutils.core import setup
with:
from setuptools import setup

Categories

Resources