安装没有父目录结构的python存储库 [英] Install python repository without parent directory structure

查看:61
本文介绍了安装没有父目录结构的python存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由很多团队继承使用的存储库,很多脚本都调用它,似乎对它进行任何结构性更改都将是一件令人头疼的事情.我想让这个 repo 以某种方式安装.它的结构如下:

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

如果它是我的存储库,我会像这样更改结构并使其可安装,然后运行python setup.py install:

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

在 setup.py 中添加一些东西,让它知道 repo 的结构像这样有趣,以便我可以安装它?

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 文件中有一个指令来设置要安装的包的名称以及它应该从哪里获取安装模块.这将让您使用所需的目录结构.例如,给定的目录结构为:

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

您可以编写一个 setup.py,例如:

You could write a setup.py such as:

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

因此,任何人都可以使用./setup.py install"或pip install"命令安装 my_repo 的内容.最终会得到 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.

作为旁注;相对导入在 python 2 和 python 3 中的工作方式不同.在后者中,任何相对导入都需要明确指定这样做的意愿.当以绝对导入方式调用时,这种安装 my_repo 的方法将在 python 3 中工作:

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

这篇关于安装没有父目录结构的python存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆