如何在不导入每个类的情况下创建具有不同类的python包

问题描述 投票:0回答:1

我在Pypi中创建了一个程序包,并进行了以下设置。可以说我的包裹叫做“ 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,
})
python package pypi
1个回答
1
投票

将类(例如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()
© www.soinside.com 2019 - 2024. All rights reserved.