How to write setup.py to include a git repo as a dependency

前端 未结 6 1924
感情败类
感情败类 2020-12-02 10:16

I am trying to write setup.py for my package. My package needs to specify a dependency on another git repo.

This is what I have so far:



        
相关标签:
6条回答
  • 2020-12-02 10:39

    You can find the right way to do it here.

    dependency_links=['http://github.com/user/repo/tarball/master#egg=package-1.0']
    

    The key is not to give a link to a git repository, but a link to a tarball. Github creates a tarball of the master branch for you if you append /tarball/master as shown above.

    0 讨论(0)
  • 2020-12-02 10:42

    The following answer is deprecated for Pip 19+


    Unfortunately the other answer does not work with private repositories, which is one of the most common use cases for this. I eventually did get it working with a setup.py file that looks like this:

    from setuptools import setup, find_packages
    
    setup(
        name = 'MyProject',
        version = '0.1.0',
        url = '',
        description = '',
        packages = find_packages(),
        install_requires = [
            # Github Private Repository - needs entry in `dependency_links`
            'ExampleRepo'
        ],
    
        dependency_links=[
            # Make sure to include the `#egg` portion so the `install_requires` recognizes the package
            'git+ssh://git@github.com/example_organization/ExampleRepo.git#egg=ExampleRepo-0.1'
        ]
    )
    

    Newer versions of pip make this even easier by removing the need to use "dependency_links"-

    from setuptools import setup, find_packages
    
    setup(
        name = 'MyProject',
        version = '0.1.0',
        url = '',
        description = '',
        packages = find_packages(),
        install_requires = [
            # Github Private Repository
            'ExampleRepo @ git+ssh://git@github.com/example_organization/ExampleRepo.git#egg=ExampleRepo-0.1'
        ]
    )
    
    0 讨论(0)
  • 2020-12-02 10:44

    I was successful with these 3 options in gitlab. I am using version 11 of gitlab.

    option 1 - no token specified. shell will prompt for username/password.

    from setuptools import setup
    
    TOKEN_VALUE = os.getenv('EXPORTED_VAR_WITH_TOKEN')
    
    setup(
        install_requires=[
            "SomePrivateLib @ git+https://gitlab.server.com/abc/SomePrivateLib.git@0.1.0#egg=SomePrivateLib"
        ]
    )
    

    option 2 - user access token specified. token generated by going to gitlab > account top right > settings > access tokens. create token with read_repository rights.

    example:

    import os
    from setuptools import setup
    
    TOKEN_VALUE = os.getenv('EXPORTED_VAR_WITH_TOKEN')
    
    setup(
        install_requires=[
            f"SomePrivateLib @ git+https://gitlab-ci-token:{TOKEN_VALUE}@gitlab.server.com/abc/SomePrivateLib.git@0.1.0#egg=SomePrivateLib"
        ]
    )
    

    option 3 - repository-level token specified. token generated by going to the repository > settings > repository > deploy tokens. from here create a token with read_repository rights.

    example:

    import os
    from setuptools import setup
    
    TOKEN_USER = os.getenv('EXPORTED_TOKEN_USER')
    TOKEN_VALUE = os.getenv('EXPORTED_VAR_WITH_TOKEN')
    
    setup(
        install_requires=[
            f"SomePrivateLib @ git+https://{TOKEN_USER}:{TOKEN_VALUE}@gitlab.server.com/abc/SomePrivateLib.git@0.1.0#egg=SomePrivateLib"
        ]
    )
    

    In all 3, I was able to do simply: "SomePrivateLib @ git+https://gitlab.server.com/abc/SomePrivateLib.git" without the #egg marking at the end.

    0 讨论(0)
  • 2020-12-02 10:58

    After digging through the pip issue 3939 linked by @muon in the comments above and then the PEP-508 specification, I found success getting my private repo dependency to install via setup.py using this specification pattern in install_requires (no more dependency_links):

    install_requires = [
      'some-pkg @ git+ssh://git@github.com/someorgname/pkg-repo-name@v1.1#egg=some-pkg',
    ]
    

    The @v1.1 indicates the release tag created on github and could be replaced with a branch, commit, or different type of tag.

    0 讨论(0)
  • 2020-12-02 11:03

    A more general answer, to get the information from the requeriments.txt file I do:

    from setuptools import setup, find_packages
    from os import path
    
    loc = path.abspath(path.dirname(__file__))
    
    with open(loc + '/requirements.txt') as f:
        requirements = f.read().splitlines()
    
    required = []
    dependency_links = []
    # do not add to required lines pointing to git repositories
    EGG_MARK = '#egg='
    for line in requirements:
        if line.startswith('-e git:') or line.startswith('-e git+') or \
                line.startswith('git:') or line.startswith('git+'):
            if EGG_MARK in line:
                package_name = line[line.find(EGG_MARK) + len(EGG_MARK):]
                required.append(package_name)
                dependency_links.append(line)
            else:
                print('Dependency to a git repository should have the format:')
                print('git+ssh://git@github.com/xxxxx/xxxxxx#egg=package_name')
        else:
            required.append(line)
    
    setup(
        name='myproject',  # Required
        version='0.0.1',  # Required
        description='Description here....',  # Required
        packages=find_packages(),  # Required
        install_requires=required,
        dependency_links=dependency_links,
    ) 
    
    0 讨论(0)
  • 2020-12-02 11:03

    Actually if you like to make your packages installable recursivelly (YourCurrentPackage includes your SomePrivateLib), e.g. when you want to include YourCurrentPackage into another one (like OuterPackage -> YourCurrentPackage -> SomePrivateLib) you'll need both:

    install_requires=[
        ...,
        "SomePrivateLib @ git+ssh://github.abc.com/abc/SomePrivateLib.git@0.1.0#egg=SomePrivateLib"
    ],
    dependency_links = [
        "git+ssh://github.abc.com/abc/SomePrivateLib.git@0.1.0#egg=SomePrivateLib"
    ]
    

    And make sure you have a tag created with your version number.

    Also if your git project is private and you like to install it inside the container e.g. Docker or GitLab runner, you will need authorized access to your repo. Please consider to use git+https with access tokens (like on GitLab: https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html):

    import os
    from setuptools import setup
    
    TOKEN_VALUE = os.getenv('EXPORTED_VAR_WITH_TOKEN')
    
    setup(
        ....
    
        install_requires=[
                ...,
                f"SomePrivateLib @ git+https://gitlab-ci-token:{TOKEN_VALUE}@gitlab.server.com/abc/SomePrivateLib.git@0.1.0#egg=SomePrivateLib"
        ],
        dependency_links = [
                f"git+https://gitlab-ci-token:{TOKEN_VALUE}@gitlab.server.com/abc/SomePrivateLib.git@0.1.0#egg=SomePrivateLib"
        ]
    )
    
    0 讨论(0)
提交回复
热议问题