简体   繁体   English

Python如何拥有一个我可以执行和导入的模块

[英]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

  • Is there a standard/best way to do this? 有没有标准/最好的方法来做到这一点?
  • Note : I don't think users need to see my class structure so I'd rather there be one user facing class which implements all of the methods in 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.

相关问题 如果我在Python中有绝对路径,如何导入模块 - How can I import module if I have an absolute path in Python 为什么我必须使用python中的电子邮件模块“从模块导入”? - Why do I have to “from module import” with the email module in python? 如何从本地 gitlab 导入模块 python? - How can I import module python from my local gitlab? 如何在 python 中懒惰地导入模块? - How can I lazily import a module in python? 如何导入 Python 模块并让它在我的主文件中工作? - How can I import a Python module and have it work within my main file? Python:如果我将路径作为字符串,如何导入模块? - Python : How to import a module if I have its path as a string? 我如何从Python脚本导入机器人关键字文件并从该文件执行关键字并生成报告 - How can i import Robot Keywords file from Python script and execute keywords from that file and generate report 如何让 Python 从另一个目录导入模块 - How To Have Python Import A Module From Another Directory 如何从python提示符执行(不导入)python脚本? - How do I execute (not import) a python script from a python prompt? 如果我也在机器上安装了python2.6和python3.1,如何从python2.5导入邮箱模块? - How to import mailbox module from python2.5 if I also have installed python2.6 and python3.1 on my machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM