1

I want to write an installer script which installs pexpect and then uses it. Something like

...
os.system('easy_install pexpect')
import pexpect
...

The problem is that import fails, with the message

import pexpect
ImportError: No module named pexpect

How can I accomplish an equivalent result?

e271p314
  • 3,841
  • 7
  • 36
  • 61
  • If you are using windows: While trying this out: have you also removed the `pexpect.pth` file in the site-packages? – User Sep 10 '13 at 11:22
  • I'm not sure I understand your comment, I'm running on linux and before I run my installer I'm deleting the pexpect package `easy_install -m pexpect` and `rm -rf /usr/lib/python2.7/site-packages/pexpect-2.4-py2.7.egg` as explained in http://stackoverflow.com/questions/1231688/how-do-i-remove-packages-installed-with-pythons-easy-install – e271p314 Sep 10 '13 at 11:47

1 Answers1

1

It will not work with setuptools, because setuptools will install pexpect as an egg, and then add it to easy-install.pth, which is checked only on startup. You can get around this in various ways, but it's easier to instead use pip to install pexpect:

>>> import pexpect
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pexpect
>>> import os
>>> os.system('bin/pip install pexpect')
Downloading/unpacking pexpect
  Downloading pexpect-2.4.tar.gz (113kB): 113kB downloaded
  Running setup.py egg_info for package pexpect

Installing collected packages: pexpect
  Running setup.py install for pexpect

Successfully installed pexpect
Cleaning up...
0
>>> import pexpect
>>>

pip will install modules in a less magical (but perhaps messier) way, and the modules end up on sys.path directly, so that works.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • Well, I expect it to work too, but I wasn't lucky as you. Fact is that in the second time I run the installer the import doesn't fail so it seems that easy_install works fine. Are you sure pexpect wasn't installed in your environment before you tried to install it and then import it in the same script? I'm using python 2.7.3 on linux fedora 17 and at the moment the above script doesn't work for me – e271p314 Sep 10 '13 at 11:41
  • @e271p314 I changed my mind, it didn't work out of the box. However, it did work with `pip install` see above. – Lennart Regebro Sep 10 '13 at 11:45