[英]Python how to have a module I can execute and import from
I have a Python package mypackage
which contains a bunch of modules / classes with this directory structure: 我有一个Python包
mypackage
,其中包含一堆具有此目录结构的模块/类:
├── interface.py
├── mypackage/
| └── __init__.py
| └── classA.py
| └── classB.py
| └── ...
The current use case is to use interface.py
with a bunch of argparse
flags: 目前的使用情况是使用
interface.py
和一帮argparse
标志:
python interface.py --foo --bar
Inside interface.py
it instantiates a bunch of the classes with mypackage
and runs their methods. 在
interface.py
它使用mypackage
实例化一堆类并运行它们的方法。 Something like: 就像是:
from classA import ClassA
def interfaceMethod(foo, bar):
a = ClassA(foo, ...)
print(a.classMethod(bar, ...)
if args.foo: interfaceMethod(args.foo, args.bar)
This works well when getting non-python / programmers to use my code. 当非python /程序员使用我的代码时,这很有效。 But I'd like to also be able to import my package within their Python code and run the same methods in
interface.py
. 但是我也希望能够在他们的Python代码中导入我的包并在
interface.py
运行相同的方法。 Something like: 就像是:
import mypackage
print(mypackage.interfaceMethod(foo, bar)
Question 题
interface.py
interface.py
所有方法 Solution 1 (I don't think this is the preferred solution): 解决方案1 (我不认为这是首选解决方案):
Add methods from interface.py
into __init__.py
: 将
interface.py
方法添加到__init__.py
:
# in __init__.py
from classA import ClassA
def interfaceMethod():
a = ClassA(foo)
print(a.classMethod(bar))
Then users can do the following in their own code (it would look very similar in interface.py
as well): 然后用户可以在自己的代码中执行以下操作(在
interface.py
看起来也非常相似):
import mypackage
mypackage.interfaceMethod()
Solution 2 : 解决方案2 :
Create a mypackage
class: 创建一个
mypackage
类:
class MyPackage():
self.classA = ClassA(...)
self.classB = ClassB(...)
def interfaceMethod():
a = self.classA()
If I create this class should I worry about the package and class having the same name? 如果我创建这个类,我应该担心包和类具有相同的名称吗? Do I change the hierarchy of the package structure to reflect that
MyPackage
is the forward facing class? 我是否更改了包结构的层次结构以反映
MyPackage
是前向类?
A good way would to use a setup.py
and use console_scripts
一个好方法是使用
setup.py
并使用console_scripts
Put you interface.py
inside you package and this to your setup.py
: 把你的
interface.py
放在你的包中,这个你的setup.py
:
setup(
# other arguments here...
entry_points={
'console_scripts': [
'script_name = my_package.interface:interfaceMethod',
],
}
)
Change your interface.py
to: 将您的
interface.py
更改为:
from classA import ClassA
def interfaceMethod(foo, bar):
a = ClassA(foo, ...)
print(a.classMethod(bar, ...)
if __name__ == '__main__':
interfaceMethod(args.foo, args.bar)
Once you install with Python setup.py install
, you can call your program from the command line: 使用
Python setup.py install
,可以从命令行调用程序:
script_name --foo --bar
For details see the full documentation . 有关详细信息,请参阅完整文档
You can still import it with: 你仍然可以用以下方法导入它:
from mypackage import interface
interface.interfaceMethod()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.