简体   繁体   English

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

[英]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 .将您的类(例如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.')

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.

相关问题 Including a Python function in an R package without importing the entire package each time the function is used - Including a Python function in an R package without importing the entire package each time the function is used Python - 将包类导入控制台全局命名空间 - Python - importing package classes into console global namespace 如何在不导入的情况下从 python 文件中获取类和函数的列表 - How to get a list of classes and functions from a python file without importing it Python-从另一个包中的一个包创建类的对象 - Python - create object of class from one package in different package 导入所有类而不在python中指定每个类 - Import all classes without specifying each one of them in python 如何在不真正导入的情况下使用长 python 模块包的别名? - How to use alias of a long python module package without really importing it? python如何在Docker中导入一个package而不先安装到容器中? - How is python importing a package in Docker without installing it in the container first? Python + Twisted…从不同文件导入类? - Python + Twisted … importing classes from different files? Python 同级导入 package 不同目录 - Python Importing package at same level but different directory 在没有任何 package 导入的情况下打印 Python 中的网格 - Print a Grid in Python without any package importing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM