构造python项目的非常简单的方法是什么? [英] What is a very *simple* way to structure a python project?
问题描述
所以我有这个需要处理文件的python东西.
首先是
my_project/
├── script.py
我只需要用python script.py file.csv
运行它即可.
然后它发展壮大:
my_project/
├── script.py
├── util/
│ └── string_util.py
├── services/
│ └── my_service.py
(每个目录中都有一个空的__init__.py
)
但是现在, my_service.py
想使用 string_util.py
,这真是不容易. >
我想在 my_service.py
中进行from ..util import string_util
(已随from services import my_service
导入到 script.py
中),但是不适用于python script.py
,因为 my_service
的__name__
仅是services.my_service
(我得到了 Attempted relative import beyond toplevel package
)
-
我可以做
cd ..
和python -m my_project.script
,但这似乎很不自然,将其放入自述文件中以说明如何运行它确实很糟糕. -
现在,我正在使用丑陋的
sys.path.append()
hack解决问题.
我还有什么其他选择?
这与观点不符,但我会分享我的看法.
您应该以不同的方式看待您的项目.选择一个执行点,然后从那里引用您的导入,以避免您尝试解决的所有奇数相对导入.因此,查看您的项目结构:
my_project/
├── script.py
├── util/
│ └── string_util.py
├── services/
│ └── my_service.py
当前正在执行的操作,请从my_project
内部执行代码.这样,您所有的进口商品应该都与该点有关.因此,您的导入实际上看起来像这样:
# my_service.py
from util.string_util import foo
另一种思考方式是,如果要移动项目或具有配置项,则需要确保指定要从中执行的项目根目录.牢记这些注意事项,并指定应在何处执行项目的单个执行点,在处理结构化包和模块并适当引用它们时将使您的工作变得更加轻松,从而允许其他系统正确使用您的项目不必处理相对奇特的相对进口.
希望这会有所帮助.
So I have this python thing that needs to process a file.
First it was:
my_project/
├── script.py
And I would simply run it with python script.py file.csv
.
Then it grew and became:
my_project/
├── script.py
├── util/
│ └── string_util.py
├── services/
│ └── my_service.py
(There is an empty __init__.py
in every directory)
But now my_service.py
would like to use string_util.py
and it's so damn not straightforward how to do this nicely.
I would like to do from ..util import string_util
in my_service.py
(which is imported into script.py
with from services import my_service
), but that does not work with python script.py
since my_service
's __name__
is then only services.my_service
(and I get the Attempted relative import beyond toplevel package
)
I can do
cd ..
andpython -m my_project.script
, but that seems so unnatural and would be really bad to put it in the README for the instructions how to run this.Right now I'm solving it with the ugly
sys.path.append()
hack.
What other options do I have?
This is bordering on opinion, but I'll share my take on this.
You should look at your project a different way. Choose one execution point, and reference your imports from there, to avoid all of the odd relative imports you are trying to work around. So, looking at your project structure:
my_project/
├── script.py
├── util/
│ └── string_util.py
├── services/
│ └── my_service.py
As you are currently doing, execute your code from within my_project
. That way all your imports should be with respect to that point. Therefore, your imports actually look like this:
# my_service.py
from util.string_util import foo
Another way to think about this, is that if you are moving your project around, or have a CI, you need to make sure you specify what is the project root you want to execute from. Keeping these things in mind, and specifying the single execution point of where your project should be executed, will make your life much easier when it comes to dealing with structuring your packages and modules and referencing them appropriately, allowing other systems to properly use your project without having to deal with odd relative imports.
Hope this helps.
这篇关于构造python项目的非常简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!