Python 导入错误:没有名为 pexpect 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22669686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:23:08  来源:igfitidea点击:

ImportError: No module named pexpect

pythonfabricpexpectfexpect

提问by mitchkman

I am using Fabric and would like to use fexpect. I have the following Python script:

我正在使用 Fabric 并想使用 fexpect。我有以下 Python 脚本:

from ilogue.fexpect import expect, expecting, run

(...)

def install_postgresql(profile):
print("!!! Installing PostgreSQL...")
print(' -> Doing pre-cleanup...')

# Remove PostgreSQL if it exists

prompts = []
prompts += expect('Do you want to continue [Y/n]? ', 'Y')

with settings(warn_only=True):
    with expecting(prompts):
        run('sudo apt-get purge postgresql')

print(' -> Doing actual installation...')

# Install PostgreSQL

prompts = []
prompts += expect('Do you want to continue [Y/n]? ', 'Y')

with expecting(prompts):
    run('sudo apt-get install postgresql')

# In some cases PostgreSQL has issues with Ubuntu's default kernel params
# that prevent PostgreSQL to start automatically, so we try to start it
# TODO: Fix it
with settings(warn_only=True):
    run('sudo service postgresql start')

When executing I get the following error:

执行时出现以下错误:

[xxx.xxx.xxx.xxx] out: Traceback (most recent call last):
[xxx.xxx.xxx.xxx] out:   File "/tmp/fexpect_MbW3QP6Zpy5KBjBGQcaYxi", line 4, in <module>
[xxx.xxx.xxx.xxx] out:     import pexpect
[xxx.xxx.xxx.xxx] out: ImportError: No module named pexpect

I am using virtualenv and pexpect is actually installed:

我正在使用 virtualenv 并且实际安装了 pexpect:

(venv)PALM00545424A:woopup i841712$ pip install pexpect
Requirement already satisfied (use --upgrade to upgrade): pexpect in ./venv/lib/python2.7/site-packages

采纳答案by mitchkman

Found the solution.

找到了解决办法。

pexpect was not part of the remote machine's Python installation.

pexpect 不是远程机器的 Python 安装的一部分。

I simply executed

我只是执行

sudo -E pip install pexpect 

on the remote machine.

在远程机器上。

回答by Ramashish Baranwal

Not a direct answer to your question, but tools like chef, puppet or salt are more suitable for installing system packages.

不是直接回答您的问题,但是像chef、puppet 或salt 这样的工具更适合安装系统包。

回答by Alexandre Mazel

In fact if your script uses fexcept, the command you need to run is actually:

事实上,如果你的脚本使用了 fexcept,你需要运行的命令实际上是:

sudo -E pip install fexpect