简体   繁体   中英

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

I created a package in Pypi and did the following setting. Lets say that my package is called "myproj". So I put all the different files in myproj/ And to use them I have to do

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 .

# 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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