Embedding a Python library in my own package

前端 未结 4 1913
臣服心动
臣服心动 2020-12-08 00:44

How can I \'embed\' a Python library in my own Python package?

Take the Requests library, for instance. How could I integrate it into my own package, the objective b

相关标签:
4条回答
  • 2020-12-08 01:18

    All of the above answers are correct but the best solution is creating a standard package.

    You can refer to this link: https://packaging.python.org/tutorials/packaging-projects/

    0 讨论(0)
  • 2020-12-08 01:25

    If it's a pure python library (no compiled modules) you can simply place the library in a folder in your project and add that folder to your module search path. Here's an example project:

    |- application.py
    |- lib
    |  `- ...
    |- docs
    |  `- ...
    `- vendor
       |- requests
       |  |- __init__.py
       |  `- ...
       `- other libraries...
    

    The vendor folder in this example contains all third party modules. The file application.py would contain this:

    import os
    import sys
    
    # Add vendor directory to module search path
    parent_dir = os.path.abspath(os.path.dirname(__file__))
    vendor_dir = os.path.join(parent_dir, 'vendor')
    
    sys.path.append(vendor_dir)
    
    # Now you can import any library located in the "vendor" folder!
    import requests
    

    Bonus fact

    As noted by seeafish in the comments, you can install packages directly into the vendor directory:

    pip install <pkg_name> -t /path/to/vendor_dir
    
    0 讨论(0)
  • 2020-12-08 01:26

    While not a direct answer to your question. You may want to look at setuptools. By leveraging this package distribution mechanism you can describe your dependencies and when your package is "installed" all the dependent packages will be installed too. You would create a setup.py file at the top of your package structure similar to:

    from setuptools import setup, find_packages
    
    setup(
        name = 'MyPackage',
        version = '1.0',
        packages = find_packages(),
        ...
        install_requires = ['requests'],
        ...
    )
    

    this would be installed by the user

    python setup.py install
    

    Requests would be automatically installed too.

    0 讨论(0)
  • 2020-12-08 01:35

    If you only need to run your application may be pyinstaller packaging is a better option.

    It will create a single bundle with everything that is needed, including Python, to avoid dependencies on the system you're running in.

    0 讨论(0)
提交回复
热议问题