繁体   English   中英

如何使用不同的类创建 python package 而无需导入每个类

[英]How to create python package with different classes without importing each one

我在 Pypi 中创建了一个 package 并进行了以下设置。 可以说我的 package 被称为“myproj”。 所以我把所有不同的文件放在 myproj/ 中我必须使用它们

from myproj.myclass1 import myclass1

我希望它像

from myproj import myproj

然后我可以使用来自不同类的所有函数,比如

myproj.class1func()
myproj.class2func()

我的设置

setup(
name="myproj",
version=VERSION,
description="PyProj package",
long_description=readme(),
long_description_content_type='text/x-rst',
url="",
author="",
author_email="",
license="MIT",
classifiers=[
],
keywords='myproj',
packages=["myproj"],
py_modules=[],
install_requires=["numpy", "matplotlib", "cvxopt", "pyswarm", "scipy", "typing", "pandas"],
include_package_data=True,
python_requires='>=3',
cmdclass={
    'verify': VerifyVersionCommand,
})

将您的类(例如myclass1myclass2 )放入文件myproj/__init__.py中。

# myproj/__init__.py
class myclass1:
    def __init__(self):
        self._thing1 = 1
    def doit(self):
        print('Hello from myclass1.')

class myclass2:
    def __init__(self):
        self._thing2 = 2
    def doit(self):
        print('Hello from myclass2.')

然后你可以这样做:

import myproj

c1 = myproj.myclass1()
c1.doit()
c2 = myproj.myclass2()
c2.doit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM