Sublime Forum

Sublimes python2.6, no virtualenv, import urandom error

#1

Situation:
Currently a (small) python library distribution is installed as part of the install in the python26.zip file. You can remove this zip-file and sublime will also look for its libraries in /lib/python2.6 (or ‘…/python26’ in pre-2211 versions)

The problem is that SublimeClang uses some additional libraries which can easily be installed on the local system.
F.e.: If you install the SublimeClang plugin you get a message like: “The ctypes lib cannot be imported. See github.com/quarnster/SublimeClang
The url suggests you use pythonbrew to fix the error. It involves linking the /lib/python2.6 to a ‘brewed’ install in ~/.pythonbrew
Instead what I did was link /lib/python2.6 to my system-wide 2.6 install: /usr/lib/python2.6.
This has solved the problems for almost a year now.

However, 2 weeks ago an update for python has triggered a problem importing ‘random’:
Lots of times:

[quote]Traceback (most recent call last):
File “./sublime_plugin.py”, line 62, in reload_plugin
File “./Package Control.py”, line 9, in
import urllib2
File “/home/tys/app/SublimeText2.2211/lib/python2.6/urllib2.py”, line 94, in
import httplib
File “/home/tys/app/SublimeText2.2211/lib/python2.6/httplib.py”, line 78, in
import mimetools
File “/home/tys/app/SublimeText2.2211/lib/python2.6/mimetools.py”, line 6, in
import tempfile
File “/home/tys/app/SublimeText2.2211/lib/python2.6/tempfile.py”, line 34, in
from random import Random as _Random
File “/home/tys/app/SublimeText2.2211/lib/python2.6/random.py”, line 47, in
from os import urandom as _urandom
ImportError: cannot import name urandom
[/quote]

This is a common error and the accepted solution is to re-build your virtual environment. (bugs.launchpad.net/ubuntu/+sour … bug/954595)

However, you cannot rebuild the environment since the interpreter is compiled into sublime_text. I tried making a new virtualenv in the python install dir (using python2.6 offcourse) and I get the same error. Including the above libs in any other python (2.6, 2.7, virtualenv or not) does NOT give me this error.
So my guess is there is a discrepancy between the build in python2.6.2 and my system-wide python2.6.8.

What is the actual problem here? Can a decent virtualenv for sublime (not having the compiled in part) fix the issue? What is the appropriate fix? Currently I’m left with the choice of using the included zip file, disabling various plugins or using the system wide python, disabling the whole of python.

Its weird though that the problem actually arised in the new python2.7 version. Why is popping up in my python2.6 install?

Using: Debian, python2.6 (minimal)+python2.7 packages. Files in /usr/lib/python2.6/lib and /usr/lib/python2.7/lib differ.

0 Likes

Clang : can't install ctypes
Clang : can't install ctypes
#2

update:
I actually tried using pythonbrew. It compiles a whole new python (2.6) in ~/.pythonbrew. The compile failed and I dont really feel like fixing the issue. Especially since I suspect this newly compiled version to also be an up-to-date version, having the same issue. And, in the end, it’s actually the same solution.

Second, it’s seems bit weird to me to having to compile (and maintain) an additional python install in .pythonbrew.

0 Likes

#3

i have the same issue(Clang : can't install ctypes) as knifter described.
would be interesting to hear from the tech support please?

0 Likes

#4

Had the same problem here. I made a quick hack solution for it.

# Hack to supply os.urandom
if not _exists("urandom"):
    def urandom(size=1):
        with file("/dev/urandom", 'rb') as f:
            return f.read(size)

Place this near the end of your os.py, just above

import copy_reg as _copy_reg

and things should start working on next restart.

0 Likes