简体   繁体   English

没有父目录结构的安装python仓库

[英]Install python repository without parent directory structure

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 : 如果是我的存储库,我将像这样更改结构并使其可安装,然后运行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? 并在setup.py中添加一些内容,以使其知道该存储库结构像这样有趣,以便我可以安装它?

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 然后您在scripts.py中的导入可能看起来像

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 . 如果您不小心,可能会弄乱您的其他一些软件包名称空间,例如,如果名为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 因此,最好是在virtualenv中运行这些脚本,并进行测试以确保命名空间不会混乱

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. setup.py文件中有一个指令,用于设置要安装的软件包的名称以及从中获取安装模块的位置。 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.py,例如:

        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 ." 因此,任何人都可以使用命令“ ./setup.py install”或“ pip install”安装my_repo的内容。 would end up with an installed copy of my_repo 's modules. 最终将获得my_repo模块的已安装副本。

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. 相对导入在python 2和python 3中的工作方式不同。在后者中,任何相对导入都需要显式指定这样做的意愿。 This method of installing my_repo will work in python 3 when calling in an absolute import fashion: 以绝对导入方式调用时,此安装my_repo的方法将在python 3中工作:

    from my_repo import scripts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM