pip install does not install required dependencies declared with install_requires

Viewed 541

My question is similar to this question, yet different.

I am the maintainer of the Python package omrdatasettools where I provide users with small helper scripts to perform dataset downloads, etc. My scripts have dependencies on their own, e.g., lxml or tqdm. I want people to be able to install my library and use it, however when you create a new conda environment and call pip install omrdatasettools the installation fails because it does not find the necessary packages.

What I've done so far: I've added all necessary libraries into the install_requires-section of my setup.py

setup(
   ...
   install_requires=['Pillow', 'muscima', "mung", "numpy", "tqdm", "scikit-image", "lxml"]
   ...
)

and I am building and distributing the package with the commands:

python setup.py sdist
twine upload --repository pypi dist/*

Uploading works as expected and now users should be able to run

conda create --name tempTest python==3.7
activate tempTest
pip install omrdatasettools

and use my library. However, when you run these lines, you get the following error:

Collecting omrdatasettools
  Downloading omrdatasettools-1.2.tar.gz (39 kB)
    ERROR: Command errored out with exit status 1:
     command: /Users/alex/opt/anaconda3/envs/tempTest/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/bs/1t82d5697dn_nd32ngbq_9bh0000gn/T/pip-install-s56i2r4e/omrdatasettools/setup.py'"'"'; __file__='"'"'/private/var/folders/bs/1t82d5697dn_nd32ngbq_9bh0000gn/T/pip-install-s56i2r4e/omrdatasettools/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/bs/1t82d5697dn_nd32ngbq_9bh0000gn/T/pip-install-s56i2r4e/omrdatasettools/pip-egg-info
         cwd: /private/var/folders/bs/1t82d5697dn_nd32ngbq_9bh0000gn/T/pip-install-s56i2r4e/omrdatasettools/
    Complete output (9 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/bs/1t82d5697dn_nd32ngbq_9bh0000gn/T/pip-install-s56i2r4e/omrdatasettools/setup.py", line 8, in <module>
        import omrdatasettools
      File "/private/var/folders/bs/1t82d5697dn_nd32ngbq_9bh0000gn/T/pip-install-s56i2r4e/omrdatasettools/omrdatasettools/__init__.py", line 3, in <module>
        from .Downloader import *
      File "/private/var/folders/bs/1t82d5697dn_nd32ngbq_9bh0000gn/T/pip-install-s56i2r4e/omrdatasettools/omrdatasettools/Downloader.py", line 9, in <module>
        from lxml import etree
    ModuleNotFoundError: No module named 'lxml'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

I read something about pip not finding the right repository to download those dependencies, but I am not sure what I am missing here. If you install all necessary dependencies first and then my package, everything works.

Any help would be highly appreciated!

1 Answers

You do import omrdatasettools in your setup.py, omrdatasettools/__init__.py imports .Downloader and omrdatasettools/Downloader.py imports lxml which is not yet installed.

Short resume: do not import (directly or indirectly) uninstalled modules in setup.py.

Ways to workaround:

  1. Do not import your submodules in __init__.py so when setup.py imports omrdatasettools it only imports __init__.py but not submodules.

  2. In your setup.py you probably only need the version so do not import it but read from a file. You can even read it from a Python module by evaluating code without triggering import from __init__.py. Like this:


from os.path import abspath, dirname, join
from setuptools import setup

versionpath = join(abspath(dirname(__file__)), 'omrdatasettools', '__version__.py')
version_dict = {}

exec(open(versionpath, 'r').read(), version_dict)

Now you have version_dict['__version__'].

Related