我有以下简单的目录结构
root
|- setup.py
|- junie.py
|- lucy.py
文件内容如下:
# setup.py
from distutils.core import setup
setup(
name='mydogs',
version='1.0',
py_modules=['junie', 'lucy']
)
# junie.py
def junie():
print('Junie says hi')
# lucy.py
def lucy():
print('Lucy says hi')
整洁。我运行build
$ python3 setup.py build
running build
running build_py
creating build
creating build/lib
copying junie.py -> build/lib
copying lucy.py -> build/lib
和install
$ python3 setup.py install
running install
/usr/local/lib/python3.10/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
/usr/local/lib/python3.10/site-packages/setuptools/command/easy_install.py:144: EasyInstallDeprecationWarning: easy_install command is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running bdist_egg
running egg_info
creating mydogs.egg-info
writing mydogs.egg-info/PKG-INFO
writing dependency_links to mydogs.egg-info/dependency_links.txt
writing top-level names to mydogs.egg-info/top_level.txt
writing manifest file 'mydogs.egg-info/SOURCES.txt'
reading manifest file 'mydogs.egg-info/SOURCES.txt'
writing manifest file 'mydogs.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-11-x86_64/egg
running install_lib
running build_py
creating build/bdist.macosx-11-x86_64
creating build/bdist.macosx-11-x86_64/egg
copying build/lib/junie.py -> build/bdist.macosx-11-x86_64/egg
copying build/lib/lucy.py -> build/bdist.macosx-11-x86_64/egg
byte-compiling build/bdist.macosx-11-x86_64/egg/junie.py to junie.cpython-310.pyc
byte-compiling build/bdist.macosx-11-x86_64/egg/lucy.py to lucy.cpython-310.pyc
creating build/bdist.macosx-11-x86_64/egg/EGG-INFO
copying mydogs.egg-info/PKG-INFO -> build/bdist.macosx-11-x86_64/egg/EGG-INFO
copying mydogs.egg-info/SOURCES.txt -> build/bdist.macosx-11-x86_64/egg/EGG-INFO
copying mydogs.egg-info/dependency_links.txt -> build/bdist.macosx-11-x86_64/egg/EGG-INFO
copying mydogs.egg-info/top_level.txt -> build/bdist.macosx-11-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/mydogs-1.0-py3.10.egg' and adding 'build/bdist.macosx-11-x86_64/egg' to it
removing 'build/bdist.macosx-11-x86_64/egg' (and everything under it)
Processing mydogs-1.0-py3.10.egg
Removing /usr/local/lib/python3.10/site-packages/mydogs-1.0-py3.10.egg
Copying mydogs-1.0-py3.10.egg to /usr/local/lib/python3.10/site-packages
mydogs 1.0 is already the active version in easy-install.pth
Installed /usr/local/lib/python3.10/site-packages/mydogs-1.0-py3.10.egg
Processing dependencies for mydogs==1.0
Finished processing dependencies for mydogs==1.0
希望我假定我的包现在已经安装,但是当我尝试导入它时,我得到以下
$ python3
Python 3.10.8 (main, Oct 13 2022, 10:18:28) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mydogs
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mydogs'
>>>
我做错了什么,为什么python找不到这个包?
你可以运行你的函数,如果你输入:
import junie
junie.junie()
如果要导入mydogs,则需要一个名为mydogs的文件。无论如何,我建议你使用https://setuptools.pypa.io/en/latest/userguide/quickstart.html
因为distutils包已弃用