Python 初始化时未导入自定义包导入模块名称
Python Custom package import module name not importing on init
我正在创建具有以下结构的自定义包:
test_package
│ README.md
│ setup.py
│
├───my_package
│ my_package.py
│ __init__.py
│
└───tests
tests.py
当导入my_package
时,我必须使用
from my_package.my_package import my_class
o = my_class()
或
import my_package
my_package.my_package.my_class()
如何只使用 from my_package import my_class
而不 添加导入到 __init__.py
?我不能那样做,因为 .
我正在使用设置工具。
非常感谢!!
import setuptools
import my_package
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
# Project information
name=my_package.__title__,
version=my_package.__version__,
long_description_content_type="text/markdown",
packages=setuptools.find_packages(),
install_requires=["pygdbmi"],
python_requires='>=3.7',
# Tests
test_suite='tests'
)
__init__.py
:
__version__ = '0.0.1'
__title__ = 'my_package'
my_package.py
from pygdbmi.gdbcontroller import GdbController
class my_class:
def __init__(self):
print("my_class!!")
self.gdbmi = GdbController()
tests.py
:
import unittest
from my_package.my_package import my_class
class some_test(unittest.TestCase):
def test_constructor(self):
self.assertIsNotNone(my_class())
if __name__ == '__main__':
unittest.main()
为了说明我的评论,
my_package.py
:
from pygdbmi.gdbcontroller import GdbController
__version__ = '0.0.1'
__title__ = 'my_package'
class my_class:
def __init__(self):
print("my_class!!")
setup.py
:
# ...
setuptools.setup(
# Project information
name=my_package.__title__,
version=my_package.__version__,
long_description_content_type="text/markdown",
py_modules=['my_package'], # replaces `packages=setuptools.find_packages(),`
install_requires=["pygdbmi"],
python_requires='>=3.7',
# Tests
test_suite='tests'
)
我正在创建具有以下结构的自定义包:
test_package
│ README.md
│ setup.py
│
├───my_package
│ my_package.py
│ __init__.py
│
└───tests
tests.py
当导入my_package
时,我必须使用
from my_package.my_package import my_class
o = my_class()
或
import my_package
my_package.my_package.my_class()
如何只使用 from my_package import my_class
而不 添加导入到 __init__.py
?我不能那样做,因为
我正在使用设置工具。
非常感谢!!
import setuptools
import my_package
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
# Project information
name=my_package.__title__,
version=my_package.__version__,
long_description_content_type="text/markdown",
packages=setuptools.find_packages(),
install_requires=["pygdbmi"],
python_requires='>=3.7',
# Tests
test_suite='tests'
)
__init__.py
:
__version__ = '0.0.1'
__title__ = 'my_package'
my_package.py
from pygdbmi.gdbcontroller import GdbController
class my_class:
def __init__(self):
print("my_class!!")
self.gdbmi = GdbController()
tests.py
:
import unittest
from my_package.my_package import my_class
class some_test(unittest.TestCase):
def test_constructor(self):
self.assertIsNotNone(my_class())
if __name__ == '__main__':
unittest.main()
为了说明我的评论,
my_package.py
:
from pygdbmi.gdbcontroller import GdbController
__version__ = '0.0.1'
__title__ = 'my_package'
class my_class:
def __init__(self):
print("my_class!!")
setup.py
:
# ...
setuptools.setup(
# Project information
name=my_package.__title__,
version=my_package.__version__,
long_description_content_type="text/markdown",
py_modules=['my_package'], # replaces `packages=setuptools.find_packages(),`
install_requires=["pygdbmi"],
python_requires='>=3.7',
# Tests
test_suite='tests'
)