创建包裹问题 [英] creating package question
问题描述
我的包目录结构如下
root-
|
Common(包含__init__.py文件)
WindowsComponents(包含__init__.py文件)
...
我希望WindowsComponents目录中的模块能够从
导入Common目录中的一些模块。在我的第一遍中,我是
能够在每个模块中附加sys.path(sys.path.append(''../ Common''))
想要从Common导入,但这感觉笨重。是否有
标准/最佳要做到这一点的方法呢?
- 里克
I have a package directory structure as follows
root-
|
Common (contains __init__.py file)
WindowsComponents (contains __init__.py file)
...
I would like modules in the WindowsComponents directory to be able to
import some modules from the Common directory. In my first pass, I was
able to append sys.path ( sys.path.append(''../Common'') ) in each module
that wants to import from Common, but this feels "clunky". Is there a
"standard"/"best" way to accomplish this?
--ERick
推荐答案
我想我有一个回答我自己的问题。在
WindowsComponents / __ init__.py文件中,我有以下内容,感觉
就像是问题的更好答案。这是否有比这更好的答案?
import os,sys
sys.path.append(os.path。 join(os.getcwd(),''Common''))
--ERICK
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?
import os, sys
sys.path.append(os.path.join(os.getcwd(), ''Common''))
--ERick
我想我有一个回答我自己的问题。在
WindowsComponents / __ init__.py文件中,我有以下内容,感觉
就像是问题的更好答案。这是否有比这更好的答案?
import os,sys
sys.path.append(os.path。 join(os.getcwd(),''Common''))
--ERICK
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?
import os, sys
sys.path.append(os.path.join(os.getcwd(), ''Common''))
--ERick
11月16日,< a href =mailto:er ********** @ comcast.net> er ********** @ comcast.net 写道:
On Nov 16, er**********@comcast.net wrote:
我有一个包目录结构如下
root-
|
Common(包含__init__.py文件)
WindowsComponents(包含__init__.py文件)
...
我希望WindowsComponents目录中的模块能够从Common目录导入一些模块。
所以你现在有一个共同的包。它可能包含一个mustard
模块。
在我的第一遍中,我能够附加sys.path(
sys.path.append (''../Common''))在每个想要从Common导入的模块中,但这感觉笨拙。
同意。您可能希望在
可能的情况下避免弄乱sys.path。
是否有标准/最佳这样做的方法是什么?
I have a package directory structure as follows
root-
|
Common (contains __init__.py file)
WindowsComponents (contains __init__.py file)
...
I would like modules in the WindowsComponents directory to be able
to import some modules from the Common directory.
So you now have a "Common" package. And it might contain a "mustard"
module.
In my first pass, I was able to append sys.path (
sys.path.append(''../Common'') ) in each module that wants to import
from Common, but this feels "clunky".
Agreed. You probably want to avoid messing with sys.path whenever
possible.
Is there a "standard"/"best" way to accomplish this?
所以root应该已经在你的sys.path / PYTHONPATH上了。
然后在说文件root / WindowsComponents / spam.py中:
来自普通进口芥末
...
mustard.attr
来自Fredrik的更多进口信息:
http://effbot.org/zone/import-confusion.htm
-
_ _ ___
| V | icah | - lliott<>< md*@micah.elliott.name
" " """
So "root" should already be on your sys.path/PYTHONPATH.
Then in say file "root/WindowsComponents/spam.py":
from Common import mustard
...
mustard.attr
More import info from Fredrik:
http://effbot.org/zone/import-confusion.htm
--
_ _ ___
|V|icah |- lliott <>< md*@micah.elliott.name
" " """
这篇关于创建包裹问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!