[英]Python module paths different after installing through pip
Suppose I am working on a python project named mymodule, and that I have the following directory structure: 假设我正在一个名为mymodule的python项目中工作,并且我具有以下目录结构:
mymodule/
README
setup.py
mymodule/
__init__.py
foo.py
bar.py
During development I normally work out of the inner mymodule
folder with all of the source files in it. 在开发过程中,我通常会在内部
mymodule
文件夹中处理所有源文件。 Because of this, if I was working on foo
and wanted to import something from bar
, I would do something like this: 因此,如果我正在研究
foo
并想从bar
导入一些东西,我会做这样的事情:
from bar import myfunction
Everything works fine during development. 在开发过程中一切正常。 The problem arises when I want to deploy my module.
当我要部署模块时会出现问题。 If I install the module into another project I'm working on, my code no longer works because the modules need to instead be imported like this:
如果我将模块安装到我正在处理的另一个项目中,则我的代码将不再起作用,因为模块需要像这样导入:
from mymodule.bar import myfunction
I could change all of my imports in mymodule
to use this format, however that would mean the module would no longer work in development. 我可以将
mymodule
所有导入内容更改为使用此格式,但是这意味着该模块在开发中将不再起作用。 When I fix the code for deployment, I break it for development, and vice versa. 修复代码以进行部署时,我会将其破坏以进行开发,反之亦然。
What am I doing wrong? 我究竟做错了什么?
I'm not sure if this is the cleanest way, but I ended up just prefixing all my imports with mymodule.
我不确定这是否是最干净的方法,但是我最终只是用
mymodule.
为所有导入添加了前缀mymodule.
. 。 If I need to develop locally, I just run
pip install .
如果我需要在本地进行开发,则只需运行
pip install .
in the root directory. 在根目录中。 If I make any changes, I need to uninstall and reinstall, but at least it works.
如果进行任何更改,则需要卸载并重新安装,但至少可以正常工作。 I'm sure there's some PATH trick that would prevent me from having to do this.
我确定有一些PATH技巧可以阻止我执行此操作。
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.