我正在像这样 pip 安装我的模块:
cd my_working_dir
pip install -e .
当我稍后从 Python 中导入模块时,我能否以某种方式检测模块是否以这种可编辑模式安装?
现在,我只是检查 os.path.dirname(mymodule.__file__))
中是否有 .git 文件夹,好吧,只有当那里确实有 .git 文件夹时才有效.有没有更靠谱的方法?
最佳答案
另一种解决方法:
在您的包中放置一个“不安装”文件。这可以是 README.md
或 not_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/