简体   繁体   English

Python 创建 pip package - 找不到模块

[英]Python creating pip package - module not found

I am trying to create a python package to distribute my code.我正在尝试创建一个 python package 来分发我的代码。 I am not getting any error in creating a package, and installing created package.在创建 package 和安装创建的 package 时,我没有收到任何错误。

However, after installation when I am trying to import the package I am getting error ModuleNotFoundError:但是,安装后,当我尝试导入 package 时,我收到错误ModuleNotFoundError:

Following is the code以下是代码

hello_world.py hello_world.py

class HelloWorld:
    def print_msg(self):
        print("Hello World")

setup.py安装程序.py

from setuptools import setup, find_packages
setup(
    name = "HelloWorld",
    version = "0.1",
    packages = find_packages(),
)

create package创建 package

▶ python setup.py bdist_wheel
running bdist_wheel
running build
installing to build/bdist.macosx-10.14-x86_64/wheel
running install
running install_egg_info
running egg_info
writing HelloWorld.egg-info/PKG-INFO
writing dependency_links to HelloWorld.egg-info/dependency_links.txt
writing top-level names to HelloWorld.egg-info/top_level.txt
reading manifest file 'HelloWorld.egg-info/SOURCES.txt'
writing manifest file 'HelloWorld.egg-info/SOURCES.txt'
Copying HelloWorld.egg-info to build/bdist.macosx-10.14-x86_64/wheel/HelloWorld-0.1-py3.7.egg-info
running install_scripts
creating build/bdist.macosx-10.14-x86_64/wheel/HelloWorld-0.1.dist-info/WHEEL
creating 'dist/HelloWorld-0.1-py3-none-any.whl' and adding 'build/bdist.macosx-10.14-x86_64/wheel' to it
adding 'HelloWorld-0.1.dist-info/METADATA'
adding 'HelloWorld-0.1.dist-info/WHEEL'
adding 'HelloWorld-0.1.dist-info/top_level.txt'
adding 'HelloWorld-0.1.dist-info/RECORD'
removing build/bdist.macosx-10.14-x86_64/wheel

Installing package安装 package

~/PycharmProjects/test_dist ▶ pip install dist/HelloWorld-0.1-py3-none-any.whl
Processing ./dist/HelloWorld-0.1-py3-none-any.whl
Installing collected packages: HelloWorld
Successfully installed HelloWorld-0.1

~/PycharmProjects/test_dist ▶ pip freeze
HelloWorld==0.1

Error While importing module导入模块时出错

>>> import HelloWorld
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'HelloWorld'

Where is hello_world.py ? hello_world.py在哪里? Is it at the root folder adjacent to setup.py ?它是否在setup.py旁边的根文件夹中? Or in some subdirectory?或者在某个子目录中? I suspect the former.我怀疑是前者。 That means you don't have any packages so find_packages() returns an empty list so setuptools don;t package any code into the package.这意味着您没有任何包,因此find_packages()返回一个空列表,因此setuptools不要将 package 任何代码放入 package。

Your hello_world.py isn't a packages (a directory with file __init__.py ), it's a standalone module and such modules must be packed using py_modules .你的hello_world.py不是一个包(一个包含文件__init__.py的目录),它是一个独立的模块,这样的模块必须使用py_modules This is how you should write your setup.py :这就是你应该如何编写你的setup.py

from setuptools import setup

setup(
    name = "HelloWorld",
    version = "0.1",
    py_modules = ['hello_world'],
)

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

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