相关疑难解决方法(0)

如何检查python模块是否存在而不导入它

我需要知道是否存在python模块,而不导入它.

导入可能不存在的东西(不是我想要的东西):

try:
    import eggs
except ImportError:
    pass
Run Code Online (Sandbox Code Playgroud)

python python-import

152
推荐指数
8
解决办法
14万
查看次数

distutils/pip中的可选依赖项

在安装我的python包时,我希望能够告诉用户各种可选的依赖项.理想情况下,我还想打印一条关于这些可选要求的信息以及它们各自的要求.

在pip或docutils的文档中我还没有看到任何东西.这些工具是否支持可选的依赖项?

python distutils pip

27
推荐指数
3
解决办法
8396
查看次数

检查是否已安装软件包

是否有一种优雅且更像Python的方法来检查Debian上是否安装了软件包?

在bash脚本中,我会这样做:

dpkg -s packagename | grep Status
Run Code Online (Sandbox Code Playgroud)

建议在Python脚本中执行相同的操作?

谢谢,

python debian

17
推荐指数
3
解决办法
1万
查看次数

如何检查是否在Python中安装了模块,如果没有,请在代码中安装它?

我想为我的代码安装模块'mutagen'和'gTTS',但我想拥有它,所以它会在没有它们的每台计算机上安装模块,但如果没有它们,它将不会尝试安装它们.他们已经安装好了.我目前有:

def install(package):
    pip.main(['install', package])

install('mutagen')

install('gTTS')

from gtts import gTTS
from mutagen.mp3 import MP3
Run Code Online (Sandbox Code Playgroud)

但是,如果您已经拥有这些模块,那么只要您打开它,就会在程序启动时添加不必要的混乱.

python module python-module python-3.x

11
推荐指数
4
解决办法
3万
查看次数

检查包是否从源树中导入

用户应该通过pip安装我们的python软件包,或者可以从github存储库中克隆它并从源代码安装。import Foo出于多种原因,用户不应从源树目录中运行,例如,缺少C扩展名(numpy具有相同的问题:在此处阅读)。因此,我们想检查用户是否正在import Foo源代码树中运行,但是如何在支持Python 3和2的情况下干净,高效且可靠地执行此操作?

Edit: Note the source tree here is defined as where the code is downloaded too (e.g. via git or from the source archive) and it contrasts with the installation directory where the code is installed too.

We considered the following:

  • Check for setup.py, or other file like PKG-INFO, which should only be present in the source. It’s not that elegant and checking for the presence of …

python package python-import

10
推荐指数
1
解决办法
337
查看次数