两个结构相同的python包使用其他包的同名函数 [英] Two python packages with the same structure use function of the same name from other package

查看:40
本文介绍了两个结构相同的python包使用其他包的同名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 2 个包,分别命名为 A 和 B.这两个包的结构相同,因为它们做的事情非常相似,例如它们的结构如下所示:

I have created 2 packages, named A and B. Both packages have the same structure to them, as they do very similar things, e.g. their structure looks like this:

A/
  __init__.py
  subpackage1/
    __init__.py
    submodule1.py
  subpackage2/
    __init__.py
    submodule2.py
  setup.py
  README.md
  requirements.txt

它们共享相同的子包、子模块和函数名称.每个模块都有一个 main 函数,它为我执行 argparsing 并使用这些参数调用一个函数.在我的 setup.py 中,我指定了额外的入口点,以便我可以从命令行调用模块:

They share the same subpackage, submodule and function names. Each module has a main function, which does the argparsing for me and calls a function with those params. In my setup.py, I specified additional entry points, so that I can call the modules from the command line:

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

with open('requirements.txt') as f:
    requirements = f.readlines() 

setuptools.setup(
    name="A",
    version="0.0.1",
    author="Me",
    author_email="me@myself.com",
    description="Test package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=setuptools.find_packages(),
    entry_points ={
        'console_scripts': [
            'command1 = subpackage1.submodule1:main',
            'command2 = subpackage2.submodule2:main'
        ]
    },
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
    install_requires = requirements
)

当我将软件包安装在一个空白的 docker 容器中时,它工作正常,我可以从命令行使用command1"和command2"调用我的函数.

When I install the package in a blank docker container, it works fine and I can call my functions with 'command1' and 'command2' from the command line.

如前所述,包 B 具有完全相同的 setup.py 文件,只是名称不同.如果我也安装它,包 A 现在使用包 B 的入口点而不是它自己的入口点.这意味着,我使用正确的名称调用了该函数,但是来自错误的包.

As stated before, package B has exactly the same setup.py file, except for the name. If I install that as well, the package A now uses the entry points of package B instead of its own. That means, I call the function with the right name, but from the wrong package.

我想将它们并排放置在我的 docker 容器中.我必须如何调整我的包裹,以便系统能够区分它们?

I want to have them side-by-side in my docker container. How do I have to adjust my packages, so that the system can differentiate between them?

我通过 pip 从我生成的轮子安装了这些包.

I installed the packages via pip from wheels that I generated.

推荐答案

第一印象,目录结构似乎不对:第一个危险信号是目录中不应该有 __init__.pysetup.py 相同的目录,第二个红旗是 setup.py 旁边的目录不能是子包,它们是顶级包.

First impression, the directory structure seems wrong: the first red flag is that there shouldn't be a __init__.py in the same directory as setup.py, the second red flag is that the directories next to setup.py can not be sub-packages, they are top-level packages.

在您的示例中,项目 A 和项目 B<中的顶级包是 subpackage1subpackage2/code> 也是如此.所以在这两种情况下,安装后可导入的项是import subpackage1import subpackage2.这也意味着,当您安装 AB 时,B 的顶级包会覆盖之前作为 A 一部分安装的包code>A(因为它们具有完全相同的名称).

In your example the top-level packages are subpackage1 and subpackage2 in the project A, and in the project B as well. So in both cases, after installation the importable items are import subpackage1 and import subpackage2. It also means that when you install A, then B, the top-level packages of B overwrite those that were previously installed as part of A (since they have the exact same name).

你可能想要做的是在项目Asetup.py旁边添加一个目录a,然后移动subpackageN 以及 __init__.py 到那个 a 目录(在项目 B 中相同).所以这个目录结构看起来像:

What you probably want to do is to add a directory a in the project A right next to its setup.py, and move both subpackageN as well as the __init__.py into that a directory (same in the project B). So that directory structure looks like:

A/
  a/
    __init__.py
    subpackage1/
      __init__.py
      submodule1.py
    subpackage2/
      __init__.py
      submodule2.py
  setup.py
  README.md
  requirements.txt

然后导入将如下所示:

import a.subpackage1
import b.subpackage1

from a import subpackage2 as subpackagea2
from b import subpackage2 as subpackageb2

接下来应该相应地调整setup.py文件:

Following that the setup.py files should be adjusted accordingly:

# ...

setuptools.setup(
    # ...
    entry_points ={
        'console_scripts': [
            'commanda1 = a.subpackage1.submodule1:main',
            'commanda2 = a.subpackage2.submodule2:main'
        ]
    },
)

这篇关于两个结构相同的python包使用其他包的同名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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