使用python3中的pip导入运行时安装的模块

2024-09-30 16:32:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在运行时安装并导入Python 3模块。在

我使用以下函数在运行时使用pip安装模块:

def installModules(modules):
    for module in modules:
        print("Installing module {}...".format(module))

        subprocess.call([sys.executable, "-m", "pip", "install", "--user", module])

模块已成功安装,但在安装完成后,我无法在运行时导入它。所以如果我这么做了:

^{pr2}$

我得到一个ModuleNotFoundError。如果在那之后,我启动了另一个python3会话,我就可以使用这些模块,例如wget,这意味着这些模块已经安装好了,但是它们对于当前的python3会话不可用。在

在python3中,是否可以在同一个python3会话中安装并导入已安装的模块,即在安装之后立即导入?在

谢谢你!在

编辑:

在VirtualBox中新安装的ubuntu19.04上,在sudo apt-get install python3-pip之后,运行以下脚本:

import os, sys
import subprocess


def installModules(modules):
    for module in modules:
        print("Installing module {}...".format(module))

        subprocess.call([sys.executable, "-m", "pip", "install", "--user", module])

def process():
    modulesToInstall = [ "wget", "zipfile2" ]
    installModules(modulesToInstall)

process()

import wget

def main():
    wget.download("http://192.168.2.234/test/configure.py")

if __name__ == "__main__":
    main()

我得到:

user@user-VirtualBox:~$ python3 script.py
Installing module wget...
Collecting wget
Installing collected packages: wget
Successfully installed wget-3.2
Installing module zipfile2...
Collecting zipfile2
  Using cached https://files.pythonhosted.org/packages/60/ad/d6bc08f235b66c11bbb76df41b973ce93544a907cc0e23c726ea374eee79/zipfile2-0.0.12-py2.py3-none-any.whl
Installing collected packages: zipfile2
Successfully installed zipfile2-0.0.12
Traceback (most recent call last):
  File "script.py", line 17, in <module>
    import wget
ModuleNotFoundError: No module named 'wget'

Python 3版本是:

user@user-VirtualBox:~$ python3 --version
Python 3.7.3

pip3版本是:

user@user-VirtualBox:~$ pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

其他信息:

user@user-VirtualBox:~$ whereis python3
python3: /usr/bin/python3.7m /usr/bin/python3.7-config /usr/bin/python3.7 /usr/bin/python3 /usr/bin/python3.7m-config /usr/lib/python3.7 /usr/lib/python3.8 /usr/lib/python3 /etc/python3.7 /etc/python3 /usr/local/lib/python3.7 /usr/include/python3.7m /usr/include/python3.7 /usr/share/python3 /usr/share/man/man1/python3.1.gz

有什么想法吗?在


Tags: 模块pipimportmodulesbinlibusrdef
1条回答
网友
1楼 · 发布于 2024-09-30 16:32:20

默认情况下,启动时Python会在模块搜索路径中添加用户站点包dir(我将其称为USPD)。但只有在文件系统(磁盘)上存在该目录时,才会发生。我没有找到任何官方文档来支持这一说法,所以我花了一些时间调试并想知道为什么事情看起来如此奇怪。在

上面的行为对这个特定的场景(pip install user)有很大的影响。考虑到将安装模块的Python进程的状态(启动时):

  1. USPD存在:

    • 事情很简单,一切都很顺利
  2. USPD不存在:

    • 模块安装将创建它
    • 但是,由于它不在模块搜索路径中,因此安装在那里的所有模块都不能用于(简单)import语句

当另一个Python进程启动时,它将属于\1。

为了解决问题,USPD应该手动添加到模块搜索路径中。以下是脚本(开头)的外观:

import sys
import os
import subprocess
import site

user_site = site.getusersitepackages()
if user_site not in sys.path:
    sys.path.append(user_site)

# ...

@EDIT0

1我刚碰到[Python]: PEP 370 Per user site-packages directory - Implementation重点是我的):

The site module gets a new method adduserpackage() which adds the appropriate directory to the search path. The directory is not added if it doesn't exist when Python is started.

相关问题 更多 >