简体   繁体   English

pip 安装与本地包具有相同命名空间的包

[英]pip installing a package with the same namespace as a local package

I am using Python 3.6.5, installed via miniconda.我正在使用 Python 3.6.5,通过 miniconda 安装。 My issue is arising from the fact that I'm pip installing a package that has the same namespace as a local package.我的问题是由于我正在 pip 安装一个与本地包具有相同命名空间的包。 After pip installing this package, I can no longer import from the local package. pip 安装此包后,我无法再从本地包中导入。 I receive a ModuleNotFoundError error.我收到ModuleNotFoundError错误。 The namespaces need to stay this way, if possible.如果可能,命名空间需要保持这种方式。

Here is my directory structure:这是我的目录结构:

/root
  stuff
    - __init__.py
    - my_stuff.py
  app.py

init .py初始化.py

__import__('pkg_resources').declare_namespace(__name__)

app.py应用程序

from stuff.my_stuff import my_fun

This works fine until I pip install the package with the same namespace, "stuff".这工作正常,直到我使用相同的命名空间“东西”安装包。 After pip installing the package, the import statement, from stuff.my_stuff import my_fun throws the following error: ModuleNotFoundError: No module named 'stuff.my_stuff' . pip 安装包后,import 语句from stuff.my_stuff import my_fun抛出以下错误: ModuleNotFoundError: No module named 'stuff.my_stuff' I kind of understand why.我有点明白为什么。 When importing modules in Python, it will look for built-in modules first, then sys.path, PYTHONPATH etc...在 Python 中导入模块时,它会先查找内置模块,然后是 sys.path、PYTHONPATH 等...

Here's the part thats really confusing me.这是真正让我感到困惑的部分。 If I create another arbitrary local module, like some_stuff , as shown below:如果我创建另一个任意本地模块,如some_stuff ,如下所示:

/root
  stuff
    - __init__.py
    - my_stuff.py
  some_stuff
    - __init__.py
    - more_stuff.py
  app.py

and if I then run:如果我然后运行:

app.py应用程序

from some_stuff.more_stuff import more_fun
from stuff.my_stuff import my_fun

Everything works as expected.一切都按预期工作。 ie if I import some_stuff.more_stuff before stuff.my_stuff , everything works.即如果我在some_stuff.more_stuff之前导入stuff.my_stuff ,一切正常。 But not vice versa.但反之则不然。 Solely importing stuff.my_stuff causes the ModuleNotFoundError .仅导入stuff.my_stuff会导致ModuleNotFoundError

app.py应用程序

# The code above works, but this causes the error
from stuff.my_stuff import my_fun

What is causing this behaviour?是什么导致了这种行为? How can I solve this issue of locally referencing a package with the same namespace as one that was pip installed?如何解决本地引用与安装 pip 具有相同命名空间的包的问题?

Edit:编辑:

I continued experimenting and noticed that when I remove all __init__.py files, everything works as expected.我继续试验并注意到当我删除所有__init__.py文件时,一切都按预期进行。 I came across this post : Since Python 3.3, a folder without an __init__.py can be considered part of an implicit namespace package.我看到了这篇文章:从 Python 3.3 开始,没有__init__.py的文件夹可以被视为隐式命名空间包的一部分。 I'm still confused about the behaviour mentioned above though.不过,我仍然对上面提到的行为感到困惑。

This SO question should answer your question这个问题应该回答你的问题

I am still putting the original answer here for convenience.为方便起见,我仍然将原始答案放在这里。


It's not possible to change "import path" (installed name) by specifying arguments to pip.无法通过为 pip 指定参数来更改“导入路径”(安装名称)。 You can, however, make some changes to the package after installing:但是,您可以在安装后对软件包进行一些更改:

  • use pip install -e git+http://some_url#egg=some-name that way even if both packages have the same import path, they will be saved under different directories (using some-name provided after #egg=).使用pip install -e git+http://some_url#egg=some-name这样即使两个包的导入路径相同,它们也会保存在不同的目录下(使用 #egg= 后提供的 some-name)。 After this you can go to the source directories of packages (usually venv/src/some-name ) and rename some folders to change import paths在此之后,您可以转到包的源目录(通常是venv/src/some-name )并重命名一些文件夹以更改导入路径

  • fork the repository, make changes, then install the package from that repository. fork 存储库,进行更改,然后从该存储库安装包。 Or you can publish your package on PyPI using different name and install it by that name或者您可以使用不同的名称在 PyPI 上发布您的包并通过该名称安装它

  • use pip download to put one of the packages in your project, then rename folders as you like使用pip download将其中一个包放入您的项目中,然后根据需要重命名文件夹

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

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