[英]How to create python package with different classes without importing each one
I created a package in Pypi and did the following setting.我在 Pypi 中创建了一个 package 并进行了以下设置。 Lets say that my package is called "myproj".
可以说我的 package 被称为“myproj”。 So I put all the different files in myproj/ And to use them I have to do
所以我把所有不同的文件放在 myproj/ 中我必须使用它们
from myproj.myclass1 import myclass1
And I would like it to work like我希望它像
from myproj import myproj
and then I could use all functions from different classes like然后我可以使用来自不同类的所有函数,比如
myproj.class1func()
myproj.class2func()
My setup我的设置
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,
})
Put your classes (eg, myclass1
and myclass2
) into the file myproj/__init__.py
.将您的类(例如
myclass1
和myclass2
)放入文件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.')
Then you can do this:然后你可以这样做:
import myproj
c1 = myproj.myclass1()
c1.doit()
c2 = myproj.myclass2()
c2.doit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.