我正在尝试测试一个包,我希望最终在PyPI上注册。不幸的是,我似乎无法在我自己的机器上正确安装它,或者至少,我无法通过import
使用该模块。我一直收到一个报告,说地点不明。最终,我正在寻找一种解决方案,不仅适用于我,而且适用于远程安装我的软件包的人,因此诸如"在路径中添加x"之类的东西;除非问题只发生在我的机器上,否则将无法工作。
目录树。两个init.py和requirements.txt都是空白的;我想打包的是coolpackage。py
.
├── __init__.py
├── MANIFEST.in
├── Makefile
├── README.md
├── examples
│ ├── example_1.py
│ ├── example_2.py
│ └── example_3.py
├── requirements-dev.txt
├── requirements.txt
├── setup.py
├── src
│ ├── __init__.py
│ ├── coolpackage.py
│ └── coolpackage.pyi
└── example4.py
py:
from setuptools import setup, find_packages
setup(
name='coolpackage',
version='0.0.2',
packages=['coolpackage'],
package_dir={'coolpackage': 'src'},
include_package_data=True,
package_data={"coolpackage": ["*.md", "*.pyi"]},
zip_safe=False,
platforms=["MacOS X", "Posix"],
classifiers=[
"Programming Language :: Python :: 3.7"
]
)
requirements-dev.txt:
flake8
wheel
setuptools
/example_3.py例子:
import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)
from coolpackage import AwesomeEntry, CoolAwesome
过程:
- 在这个repo上面的一个文件夹中创建并激活一个新的venv
- cd到这个仓库
pip3 install -r requirements-dev.txt
python3 setup.py bdist_wheel
pip3 install dist/coolpackage-0.0.2-py3-none-any.whl
flake8 --ignore E501,E231,E128 src/coolpackage.py
python3 examples/example_3.py
最后一个命令python3 examples/example_3.py
打印了这个,表明coolpackage存在并且已经安装,但是我不明白为什么不能导入它。
['coolpackage==0.0.2', 'flake8==4.0.1', 'importlib-metadata==4.2.0', 'mccabe==0.6.1', 'pip==20.1.1', 'pycodestyle==2.8.0', 'pyflakes==2.4.0', 'setuptools==47.1.0', 'typing-extensions==4.2.0', 'wheel==0.37.1', 'zipp==3.8.0']
Traceback (most recent call last):
File "examples/example_3.py", line 7, in <module>
from coolpackage import AwesomeEntry, CoolAwesome
ImportError: cannot import name 'AwesomeEntry' from 'coolpackage' (unknown location)
我已经尝试过的东西
其他东西:
- 删除顶层initpy
- 重命名包和所有非init.py python文件到其他文件
- 删除和重新创建venv
- 将venv与repo本身放在同一个文件夹
- 使用find_packages()在setup.py
- 更新pip
- 使用pip代替pip3
- 使用python而不是python3来运行example_3.py(有趣的是,这列出了更多的包,并抛出
ModuleNotFoundError: No module named 'coolpackage'
而不是ImportError;再深入研究一下,发现which python
和which python3
的输出并不等效,因为它们来自venv中的不同文件夹,但python --version
和python3 --version
是等效的。这让我觉得venv本身可能是坏的。)
类似的问题似乎没有帮助:
- importterror: cannot import name (unknown location)——>我尝试进入
examples/
目录并运行python3 example_1.py
,但我得到同样的错误。还尝试将example_3.py移动到venv文件夹所在文件夹上方的一个完全不同的文件夹,也没有工作。 - setup.py安装包无法导入——>我用的是管理员账号。我还尝试了chmod 777的整个venv,但这没有帮助,我没有使用通配符像有人在回答建议中提到的。
- pip安装成功,但Python说没有找到模块——>我已经使用了pip3
相关输出,即向墙上扔东西并希望有东西粘在墙上
#4的输出(python3 setup.py bdist_wheel):running bdist_wheel
running build
running build_py
package init file 'src/__init__.py' not found (or not a regular file)
creating build
creating build/lib
creating build/lib/coolpackage
copying src/ __init__.py -> build/lib/coolpackage
copying src/coolpackage.py -> build/lib/coolpackage
running egg_info
creating coolpackage.egg-info
writing coolpackage.egg-info/PKG-INFO
writing dependency_links to coolpackage.egg-info/dependency_links.txt
writing top-level names to coolpackage.egg-info/top_level.txt
writing manifest file 'coolpackage.egg-info/SOURCES.txt'
reading manifest file 'coolpackage.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'coolpackage.egg-info/SOURCES.txt'
copying src/coolpackage.pyi -> build/lib/coolpackage
installing to build/bdist.macosx-10.9-x86_64/wheel
running install
running install_lib
creating build/bdist.macosx-10.9-x86_64
creating build/bdist.macosx-10.9-x86_64/wheel
creating build/bdist.macosx-10.9-x86_64/wheel/coolpackage
copying build/lib/coolpackage/ __init__.py -> build/bdist.macosx-10.9-x86_64/wheel/coolpackage
copying build/lib/coolpackage/coolpackage.pyi -> build/bdist.macosx-10.9-x86_64/wheel/coolpackage
copying build/lib/coolpackage/coolpackage.py -> build/bdist.macosx-10.9-x86_64/wheel/coolpackage
running install_egg_info
Copying coolpackage.egg-info to build/bdist.macosx-10.9-x86_64/wheel/coolpackage-0.0.2-py3.7.egg-info
running install_scripts
creating build/bdist.macosx-10.9-x86_64/wheel/coolpackage-0.0.2.dist-info/WHEEL
creating 'dist/coolpackage-0.0.2-py3-none-any.whl' and adding 'build/bdist.macosx-10.9-x86_64/wheel' to it
adding 'coolpackage/ __init__.py'
adding 'coolpackage/coolpackage.py'
adding 'coolpackage/coolpackage.pyi'
adding 'coolpackage-0.0.2.dist-info/METADATA'
adding 'coolpackage-0.0.2.dist-info/WHEEL'
adding 'coolpackage-0.0.2.dist-info/top_level.txt'
adding 'coolpackage-0.0.2.dist-info/RECORD'
removing build/bdist.macosx-10.9-x86_64/wheel
package init file 'src/__init__.py' not found (or not a regular file)
错误是一种sus,但我认为这可能只是因为它是空白的?
pip安装车轮后pip冻结输出:
coolpackage @ file:///Users/myverycoolusername/Repos/coolcoolcool/dist/coolpackage-0.0.2-py3-none-any.whl
flake8==4.0.1
importlib-metadata==4.2.0
mccabe==0.6.1
pycodestyle==2.8.0
pyflakes==2.4.0
typing-extensions==4.2.0
zipp==3.8.0
which pip, which python,等等,都在同一个venv
>which python
/Users/myverycoolusername/Repos/venv/bin/python
>which python3
/Users/myverycoolusername/Repos/venv/bin/python3
>python --version
Python 3.7.9
>python3 --version
Python 3.7.9
>which pip
/Users/myverycoolusername/Repos/venv/bin/pip
>which pip3
/Users/myverycoolusername/Repos/venv/bin/pip3
>pip --version
pip 20.1.1 from /Users/myverycoolusername/Repos/venv/lib/python3.7/site-packages/pip (python 3.7)
>pip3 --version
pip 20.1.1 from /Users/myverycoolusername/Repos/venv/lib/python3.7/site-packages/pip (python 3.7)
我没有PYTHONPATH环境变量,我想我不应该需要一个,因为我在venv中。
感谢@sinoroc和盯着这个答案看了一会儿,我明白了。需要进行以下更改:
- 在根目录删除
__init__.py
- 将src重命名为coolpackage
- 从source.py中删除package_dir
- 在example_3.py,
from coolpackage import coolpackage
,然后访问AwesomeEntry作为coolpackage.AwesomeEntry