Install python repository without parent directory structure

假装没事ソ 提交于 2021-02-11 02:57:55

问题


I have a repository I inherited used by a lot of teams, lots of scripts call it, and it seems like its going to be a real headache to make any structural changes to it. I would like to make this repo installable somehow. It is structured like this:

my_repo/
    scripts.py

If it was my repository, I would change the structure like so and make it installable, and run python setup.py install:

my_repo/
    setup.py
    my_repo/
        __init__.py
        scripts.py

If this is not feasible (and it sounds like it might not be), can I somehow do something like:

my_repo/
    setup.py
    __init__.py
    scripts.py

And add something to setup.py to let it know that the repo is structured funny like this, so that I can install it?


回答1:


You can do what you suggest.

my_repo/
    setup.py
    __init__.py
    scripts.py

The only thing is you will need to import modules in your package via their name if they are in the base level. So for example if your structure looked like this:

my_repo/
    setup.py
    __init__.py
    scripts.py
    lib.py
    pkg/
        __init__.py
        pkgmodule.py

Then your imports in scripts.py might look like

from lib import func1, func2
from pkg.pkgmodule import stuff1, stuff2

So in your base directory imports are essentially by module name not by package. This could screw up some of your other packages namespaces if you're not careful, like if there is another dependency with a package named lib. So it would be best if you have these scripts running in a virtualenv and if you test to ensure namespacing doesn't get messed up




回答2:


There is a directive in setup.py file to set the name of a package to install and from where it should get it's modules for installation. That would let you use the desired directory structure. For instance with a given directory structure as :

     my_repo/
       setup.py
       __init__.py
       scripts.py

You could write a setup.py such as:

        setup(
        # -- Package structure ----
        packages=['my_repo'],
        package_dir={'my_repo': '.'})

Thus anyone installing the contents of my_repo with the command "./setup.py install" or "pip install ." would end up with an installed copy of my_repo 's modules.

As a side note; relative imports work differently in python 2 and python 3. In the latter, any relative imports need to explicitly specify the will to do so. This method of installing my_repo will work in python 3 when calling in an absolute import fashion:

    from my_repo import scripts


来源:https://stackoverflow.com/questions/36847554/install-python-repository-without-parent-directory-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!