Python: no module named contextlib?

Does anyone know where I can find this python module 'contextlib'?

root@overo :~# python Python 2.6.6 (r266:84292, Mar 9 2011, 10:05:36) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import contextlib Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named contextlib 

I myself did not compile python myself. I'm just in this mess. It runs on an ARM processor, so maybe some things have been left to save space.

I ran find / | grep contextlib find / | grep contextlib , resulting in nothing happening.

Can I download this module from somewhere and just put it in / usr / lib / python 2.6? Will this work?

+6
python
source share
6 answers

As others noted, this module should be in the standard library, but if it is an embedded device, it may have been removed to save space (if true, a stupid IMO choice, since it leaves contextlib.contextmanager t21> exposing most of its power and facilities)

If you can name a specific device or manufacturer (or contact the supplier directly), you can get the best answer.

As for the commit, grabbing http://hg.python.org/cpython/file/2.6/Lib/contextlib.py and deleting it in sys.path somewhere should do the trick (running python -m site will reset the list directories you can use)

+7
source share

It was part of the standard library from 2.5 to docs . It seems a little strange that you do not have it, it works with 2.6.6 for me (Ubuntu 10.10):

 blair@blair-eeepc :~$ python Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import contextlib >>> contextlib.__file__ '/usr/lib/python2.6/contextlib.pyc' 

Someone might have a better suggestion, but if it comes to it, there is a link at the top of the documentation for the source code (which is Python, so you can use it directly without any compilation or anything else).

Change If, as Santiago Lezica suggested, you compiled your copy of Python manually, in which case you just need to copy the module to the correct library path.

Edit for updated question . As far as I know, simply disconnecting the source to a directory in the Python path should work. You can do this in the system library, but in order to avoid deleting / replacing / otherwise in future updates, I would recommend placing it in a separate directory and adding this directory to the Python path. You can put it under / usr / local or somewhere in your home directory.

+4
source share

With Angsrom Linux, a context list is included in the python-misc package. You can capture it by doing:

 opkg install python-misc 

This, however, will not give you all the expected python modules, so you can also install python modules:

 opkg install python-modules 
+2
source share

contextlib was introduced in Python 2.5, can you install Python 2.6.6 again and again? From my copy of Python 2.6.6:

 Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import contextlib >>> 
0
source share

Python 2
sudo apt-get install python-contextlib2

Python 3
sudo apt-get install python3-contextlib2

0
source share

Check sys.path to make sure your python interpreter is looking in the correct directories. It should look something like this (not necessarily identical):

 >>> import sys >>> sys.path ['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/pymodules/python2.6'] 

EDIT: with updated information on the question that this is an installation of unknown origin on a limited device, assuming that unnecessary modules were removed to save space, it makes sense. However, for the record I’ll talk about another, perhaps more common scenario, when the modules cannot be found: when there are problems with file permissions. For instance:

 $ python -c 'import contextlib; print(contextlib.__file__)' /usr/lib/python2.6/contextlib.pyc $ ls -l /usr/lib/python2.6/contextlib.py* -rw-r--r-- 1 root root 4136 Dec 26 16:42 /usr/lib/python2.6/contextlib.py -rw-r--r-- 1 root root 4127 Jan 1 21:45 /usr/lib/python2.6/contextlib.pyc $ sudo chmod go-r /usr/lib/python2.6/contextlib.py* $ python -c 'import contextlib; print(contextlib.__file__)' Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named contextlib 

Particularly with custom installations, import problems due to file permissions and content issues are some of the easiest things to check and, as a rule, to fix.

-1
source share

All Articles