在VENV内部无法导入setup.py模块

发布于 2025-01-29 18:51:07 字数 6822 浏览 2 评论 0原文

我正在尝试测试我希望最终在PYPI上注册的软件包。不幸的是,我似乎无法在自己的计算机上正确安装它,或者至少我无法通过import使用该模块。我一直在援引未知位置的情况下,我一直在获得无所事事。最终,我正在寻找一种不仅适用于我的解决方案,而且对于远程安装包装的人,因此“将X添加到路径”之类的东西不起作用,除非问题只会在我的机器上发生。

仓库的树。 init .py都是空白的,sumplions.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

setup.py:insources-dev.txt:

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"
    ]
)

示例

flake8
wheel
setuptools

/示例_3.py:process:

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

  1. 此repo cd上方的一个文件夹中制作和激活此repo
  2. cd中的一个新鲜的VENV,以
  3. pip3安装-r Euncess-dev.txt
  4. python3 setup.py bdist_wheel
  5. pip3 install dist dist/coolpackage-0.0.2-py3-none-anone.whl
  6. > flake8 - ignore e501,e231,e128 src/coolpackage.py
  7. python3示例/example_3.py

最后命令,python3示例/example_3.py_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)

我尝试过的

一些东西:

  • 删除顶级 init .py
  • 将包装重命名和所有非 - init .py python文件中的其他内容
  • 删除和重新创建VENV的
  • 内容VENV在与repo本身同一文件夹中
  • 使用find_packages()中的setup.py.py
  • 使用pip
  • 使用pip3而不是
  • 使用python代替python3来运行示例example_3.py(有趣的是,这列出了更多的软件包,并抛出一个modulenotfounderror :没有名为“ coolpackage'的模块,而不是Ingrorror;它们来自VENV中的不同文件夹,但是python - versionpython3-version是等效的

。这似乎没有帮助:

相关的输出,即,在墙上投掷东西,并希望某些东西贴

在#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> package init'src/__ src/__ Init__.py'找不到(或找不到(或找不到)不是常规文件)错误是SUS,但我认为这可能只是因为它是空白的吗?

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

哪个PIP,哪个Python等,所有PIP在同一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环境变量,而AFAIK我不需要一个,因为我在VENV中。

I'm trying to test a package that I hope to eventually register on PyPI. Unfortunately, I can't seem to install it correctly on my own machine, or at least, I cannot use the module via import. I keep getting an ImportError citing unknown location. Ultimately I am looking for a solution that will work not just for me, but for people pip installing my package remotely, so something like "adding x to the path" isn't going to work unless the problem will only happen on my machine.

Tree of the repo. Both init.py are blank and so is requirements.txt; the thing I actually want to package is 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

setup.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

examples/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

Process:

  1. Make and activate a fresh venv in one folder above this repo
  2. cd into this repo
  3. pip3 install -r requirements-dev.txt
  4. python3 setup.py bdist_wheel
  5. pip3 install dist/coolpackage-0.0.2-py3-none-any.whl
  6. flake8 --ignore E501,E231,E128 src/coolpackage.py
  7. python3 examples/example_3.py

The last command, python3 examples/example_3.py, prints this, indicating that coolpackage exists and is installed, but I don't see why it can't be imported.

['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)

Stuff I Tried Already

Miscellaneous stuff:

  • Deleting the top level init.py
  • Renaming the package and all non-init.py python files to something else
  • Deleting and recreating the venv
  • Putting the venv in the same folder as the repo itself
  • Using find_packages() in setup.py
  • Updating pip
  • Using pip instead of pip3
  • Using python instead of python3 to run example_3.py (interestingly, this lists way more packages, and throws a ModuleNotFoundError: No module named 'coolpackage' instead of an ImportError; looked a little deeper and found that the output of which python and which python3 are not equivalent in that they originate from different folders in the venv, but python --version and python3 --version are equivalent. This has me thinking the venv itself might be borked.)

Similar questions that don't seem to help:

Related Output, i.e., hurling things at the wall and hoping something sticks

Output of #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

The package init file 'src/__init__.py' not found (or not a regular file) error is kind of sus, but I think that might just be because it's blank?

Output of pip freeze after pip installing the wheel:

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, etc, all within the same 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)

I do not have a PYTHONPATH environmental variable and afaik I should not need one because I am in a venv.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2025-02-05 18:51:07

感谢@sinoroc,并有点盯着这个答案,我想出了这一点。需要以下更改:

  • 在root
  • 重命名src to coolpackage
  • 上删除__ init py 从emoce_3.py中删除package_dir
  • ,从coolpackage incool import coolpackage AS coolpackage.awesomeentry

Thanks to @sinoroc and a bit of staring at this answer, I figured this out. The following changes were needed:

  • removing __init__.py at the root
  • renaming src to coolpackage
  • removing package_dir from source.py
  • in example_3.py, from coolpackage import coolpackage, then access AwesomeEntry as coolpackage.AwesomeEntry
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文