简体   繁体   English

Python app install via pip3 不安装源包

[英]Python app install via pip3 does not install source packages

Here is my python project structure这是我的python项目结构

▶ tree -L 2
.
├── Dockerfile
├── README.md
├── my_app
│   ├── __init__.py
│   ├── main.py
│   ├── settings.py
│   ├── utils.py
│   └── views.py

And part of my setup.py我的setup.py的一部分

    package_dir={'': 'my_app'},
    packages=find_packages(where='my_app'),
    include_package_data=True,
    python_requires='>=3.9',
    entry_points={
        'console_scripts': [
            'my-app=my_app.main:run',
        ],
pip3 install .  --force-reinstall

runs OK and I am in a virtualenv.运行正常,我在 virtualenv 中。

However, when trying to run the program但是,当尝试运行程序时

Traceback (most recent call last):
  File "/Users/myuser/myfolder/.venv/bin/my-app", line 5, in <module>
    from my_app.main import run
ModuleNotFoundError: No module named 'my_app'

I also noticed that in ls.venv/lib/python3.9/site-packages/ there are no source files of my-app , only my_app-0.0.1.dist-info file我还注意到在ls.venv/lib/python3.9/site-packages/没有my-app的源文件,只有my_app-0.0.1.dist-info文件

Why is that?这是为什么?

You misunderstand how find_packages(where='my_app') work, see this :您误解了find_packages(where='my_app')工作原理, 参见:

find_packages(where='.', exclude=()) find_packages(where='.', exclude=())
Return a list all Python packages found within directory 'where'返回目录“where”中找到的所有 Python 包的列表

This means it doesn't mean try to find the package my_app from current folder, but try to find packages from the folder my_app , in your case, no package will be found in my_app .这意味着这并不意味着尝试从当前文件夹中查找 package my_app ,而是尝试从my_app文件夹中查找包,在您的情况下,不会在my_app中找到 package 。

So, for your case, next is enough:因此,对于您的情况,下一个就足够了:

setup.py :设置.py

from setuptools import setup, find_packages

setup(
    name="my-app",
    version="0.0.1",

    packages=find_packages(),
    include_package_data=True,
    entry_points={
        'console_scripts': [
            'my-app=my_app.main:run',
        ],
    }
)

Additional, if your main intention is just to include my_app , bypass others, then find_packages(include=[ syntax is what you are looking for:另外,如果您的主要意图只是包含my_app ,绕过其他人,那么find_packages(include=[语法就是您要寻找的:

setup.py :设置.py

from setuptools import setup, find_packages

setup(
    name="my-app",
    version="0.0.1",

    packages=find_packages(include=['my_app']),
    include_package_data=True,
    entry_points={
        'console_scripts': [
            'my-app=my_app.main:run',
        ],
    }
)

NOTE , don't use package_dir={'': 'my_app'} , it will try to find my_app.my_app , unless you use package_dir={'my_app': 'my_app'} , but it's meaningless in your case.注意,不要使用package_dir={'': 'my_app'} ,它会尝试找到my_app.my_app ,除非你使用package_dir={'my_app': 'my_app'} ,但在你的情况下它没有意义。

is not it the solution?这不是解决方案吗? pip installing in global site-packages instead of virtualenv I had smth similar on worked machine, requirements satisfied on installing, but module not found on run, removed that package globally and installed again. pip 安装在全局站点包中而不是 virtualenv我在工作机器上有类似的东西,安装要求满足,但运行时找不到模块,全局删除 package 并再次安装。 Might will help可能会有所帮助

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

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