13

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

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:

(venv)PALM00545424A:woopup i841712$ pip install pexpect
Requirement already satisfied (use --upgrade to upgrade): pexpect in ./venv/lib/python2.7/site-packages
mitchkman
  • 6,201
  • 8
  • 39
  • 67

4 Answers4

20

Found the solution.

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

I simply executed

sudo -E pip install pexpect 

on the remote machine.

mitchkman
  • 6,201
  • 8
  • 39
  • 67
  • Glad you found a solution, but it should not be necessary to have pexpect installed on the remote. The module usually gets sent along with the fexpect command script. – Jasper van den Bosch Mar 26 '14 at 18:49
  • If you would like to take this further feel free to post this as an issue at https://github.com/ilogue/fexpect/issues?state=open it would be helpful to see the output of `ls /tmp` on the remote – Jasper van den Bosch Mar 26 '14 at 18:52
  • Sorry for the incoming caps... YOU SHOULD NEVER USER `sudo pip install....` unless you are a system admin and know what you are doing. Instead, use `pip install --user ...` if you don't want to risk corrupting your system distro. Also if you are in a venv just use `pip install ....` – Jakob Guldberg Aaes Oct 05 '21 at 16:12
3

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

sudo -E pip install fexpect 
Alexandre Mazel
  • 2,462
  • 20
  • 26
0

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

Ramashish Baranwal
  • 7,166
  • 2
  • 18
  • 14
  • Yes, I have already considered it, but Fabric is much more lightweight than e.g. Chef and more straight-forward – mitchkman Mar 26 '14 at 18:50
0

I got the same error when using pexpect lib to interact with gatttool. I used Pycharm to remote debug code on Raspberry pi. Here is the command processed by Pycharm and the error output

sudo+ssh://pi3@192.168.x.x:22/usr/bin/python3 -u /tmp/pycharm_project_55/Rasp_Pi/BluetoothBLEComm.py
Traceback (most recent call last):
  File "/tmp/pycharm_project_55/Rasp_Pi/BluetoothBLEComm.py", line 33, in <module>
    import pexpect
ModuleNotFoundError: No module named 'pexpect'

After spending few hours, I identified the issue is with the option I checked while configuring the Remote Python Interpreter in Pycharm. It is the option that executes code with root privileges via sudo.

**sudo**+ssh://pi3@192.168.x.x:22/usr/bin/python3...

The pexpect package was only installed for my local pi3 user. So to solve the issue either I had to install pexpect using sudo or uncheck the option that executes the code with root privileges.

Srini R
  • 1
  • 1