简体   繁体   English

无法加载 Python 包的子模块:ModuleNotFoundError: No module named

[英]Unable to load submodules of a Python package: ModuleNotFoundError: No module named

I am new to Python.我是 Python 的新手。 This looks like a very simple problem but I am unable to solve it after trying my best.这看起来是一个非常简单的问题,但我尽力了却无法解决。

I am trying to publish a python package that I developed to an artifact store.我正在尝试将我开发的 python 包发布到工件商店。 However, when I download the package on a target machine, it runs into the error about inner modules not found.但是,当我在目标机器上下载该软件包时,它遇到了有关未找到内部模块的错误。 The packaging and installation both look good.包装和安装都很好。 The output messages show that it does include the submodules.输出消息显示它确实包含子模块。

I have a directory structure as per below.我有一个如下所示的目录结构。

samplepackage/
        hello.py
        __init__.py
        dir1/
            __init__.py
           dir1pkg.py

Below are the contents of the files.下面是文件的内容。 The init files are empty. init 文件是空的。

hello.py你好.py

import sys
from dir1.dir1pkg import dir1pkg

def main ():
    dirpkg = dir1pkg('This is msg')
    dirpkg.printmsg()

if __name__ == "__main__":
    main()

dir1pkg.py目录1pkg.py

class dir1pkg:
    def __init__(self,msg):
        self.msg = msg
        
    def printmsg(self):
        print(self.msg)

setup.py设置文件

import setuptools
from setuptools import setup, find_packages, find_namespace_packages

setup(
    name="samplepackage", 
    version="0.0.3",
    author="myname",
    author_email="myemail@email.com",
    description="This is a sample package",
    long_description="This is long description",
    long_description_content_type="text/markdown",
    packages=setuptools.find_packages(),
    include_package_data=True,
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    entry_points={
        "console_scripts":[
            "samplepackage=samplepackage.hello:main"
        ]
    }
    ,python_requires='>=3.7'
)

Below is how I am packaging and publishing to artifacts repo.下面是我如何打包和发布到 artifacts 仓库。

python setup.py sdist bdist_wheel
twine upload --config-file ".pypirc" -r <artifact_feed> dist/*

Below is how I am installing on the target.以下是我在目标上的安装方式。

python -m pip install --upgrade samplepackage
python -m SamplePackage.hello.py

This gives me the error below这给了我下面的错误

C:\Users\manan\Desktop>python -m samplepackage.hello.py
Traceback (most recent call last):
  File "C:\Users\manan\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 185, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "C:\Users\manan\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "C:\Users\manan\AppData\Local\Programs\Python\Python38\lib\site-packages\samplepackage\hello.py", line 2, in 
    from dir1.dir1pkg import dir1pkg
ModuleNotFoundError: No module named 'dir1'

However, this runs just fine from where I am developing the package.但是,这从我开发包的地方运行得很好。 I can execute below and it is able to find the inner module without any issues.我可以在下面执行,它能够毫无问题地找到内部模块。

C:\Users\mdmehta\Desktop\PythonPackage\samplepackage>python hello.py
This is msg

I have tried doing a lot of twicks around setup.py but none of them work.我试过在 setup.py 周围做了很多 twicks,但没有一个工作。 Even the output of the installed package looks good.甚至安装包的输出看起来也不错。 I do see dir1 being included as a package.我确实看到 dir1 被包含在一个包中。

>>> help('samplepackage')
Help on package samplepackage:

NAME
    samplepackage

PACKAGE CONTENTS
    dir1 (package)
    hello

FILE
    c:\users\mdmehta\appdata\local\programs\python\python38\lib\site-packages\samplepackage\__init__.py

Figured out the problem.想通了问题所在。 We have to use full imports for it to work.我们必须使用完全导入才能使其工作。

The file hello.py should use from samplepackage.dir1.dir1pkg import dir1pkg文件 hello.py 应该使用from samplepackage.dir1.dir1pkg import dir1pkg

Do not use Visual studio for python projects.不要将 Visual Studio 用于 Python 项目。 It does not like fully qualified package names.它不喜欢完全限定的包名。 Switched to pycharm and modified the package imports to fully qualified ones and everything started working.切换到 pycharm 并将包导入修改为完全限定的包,一切开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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