How do I handle importing third-party libraries in my setup.py script?

I am developing a Python application and in the process of deploying the release. I have a PyPI server configured on a company server, and I copied the source distribution of my package to it.

I checked that the package is hosted on the server, and then tried to install 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 because I'm trying to import a third-party library appdirsinto mine setup.py, which is necessary to calculate the argument data_filesfor 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 do not have appdirsit installed on my local development computer, and I do not expect end users to have this.

, , setup.py, , ? , appdirs setup.py, , appdirs ?

+4
3

. , , , .

, , setup.py

, , , , . , .

?

3 :

  • (, pip ). setuptools ez_setup.py, setuptools. , appdirs.

  • (, ) . , appdirs - . . , !

  • , . :

    try:
        import appdirs
    except ImportError:
        raise ImportError('this package requires "appdirs" to be installed. '
                          'Install it first: "pip install appdirs".')
    
+2

install_requires . , python . requirements.txt, , "pip install -r"

+1

pip , import:

try:
    import appdirs
except ImportError:
    import pip
    pip.main(['install', 'appdirs'])
    import appdirs

importlib __import__ pip.main PATH. , .

" python " , setup.py, .

0

Source: https://habr.com/ru/post/1678344/


All Articles