python - 如何检测模块是否安装在 "editable mode"中?

标签 python pip

我正在像这样 pip 安装我的模块:

cd my_working_dir
pip install -e .

当我稍后从 Python 中导入模块时,我能否以某种方式检测模块是否以这种可编辑模式安装?

现在,我只是检查 os.path.dirname(mymodule.__file__)) 中是否有 .git 文件夹,好吧,只有当那里确实有 .git 文件夹时才有效.有没有更靠谱的方法?

最佳答案

另一种解决方法:

在您的包中放置一个“不安装”文件。这可以是 README.mdnot_to_install.txt 文件。使用任何非 pythonic 扩展名,以防止安装该文件。然后检查您的包中是否存在该文件。

建议的源代码结构:

my_repo
|-- setup.py
`-- awesome_package
    |-- __init__.py
    |-- not_to_install.txt
    `-- awesome_module.py

设置.py:

# setup.py
from setuptools import setup, find_packages

setup(
    name='awesome_package',
    version='1.0.0',

    # find_packages() will ignore non-python files.
    packages=find_packages(),
)

__init__.py 或 awesome_module.py:

import os

# The current directory
__here__ = os.path.dirname(os.path.realpath(__file__))

# Place the following function into __init__.py or into awesome_module.py

def check_editable_installation():
    '''
        Returns true if the package was installed with the editable flag.
    '''
    not_to_install_exists = os.path.isfile(os.path.join(__here__, 'not_to_install.txt'))
    return not_to_install_exists

关于python - 如何检测模块是否安装在 "editable mode"中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43348746/

相关文章:

python - 从 S3 读取 ZIP 文件而不下载整个文件

python - 如何从网络链接列表中检索 URL 和 URL 中的数据

python - 如何确定 setup.py 中实际需要哪些要求?

pip - 如何完全卸载jupyter

python - Python 3.2 中的外来字符

python - 一段时间后做某事 : Pygame

python - 找不到 'pip==9.0.1' 分布,应用程序需要该分布

python - 为在 AWS Lambda 中使用的 python 包指定 C(++) 依赖项

python - 自动添加已安装的 PIP 包到路径

python - 如何使用 pip 在 Windows 7 上使用 MinGW-w64 编译器安装包?