Skip to content
This repository

ImportError: cannot import name urandom #87

Closed
nnutter opened this Issue · 13 comments

11 participants

Nathaniel Nutter Kirill Klenov danielsdesk Lx Yu Luke Orland Aaron O'Leary Edward Liaw snoby Evan Wheeler Hari Jayaram Sebastian Dahlgren
Nathaniel Nutter

I just installed python-mode to try it out but I get this error:

Error detected while processing /Users/nnutter/.homesick/repos/nnutter/castle/home/.vim/bundle/python-mode/plugin/pymode.vim:
line  220:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/nnutter/.homesick/repos/nnutter/castle/home/.vim/bundle/python-mode/pylibs/ropevim.py", line 3, in <module>
    import tempfile
  File "/usr/local/Cellar/python/2.7.3/lib/python2.7/tempfile.py", line 34, in <module>
    from random import Random as _Random
  File "/usr/local/Cellar/python/2.7.3/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

I performed each of the imports in a python shell and did not receive any errors. I didn't see any previous issues for this so I am submitting a new issue. Perhaps it is just due to my environment (OS X, Homebrew, etc.) but I don't know what next steps to take. Please let me know if I can provide any other info that would be useful.

Kirill Klenov
Owner
klen commented

Maybe others OSX users can be help.

Nathaniel Nutter
nnutter commented

Completely uninstalling MacVim and Python (via Homebrew) and then installing MacVim and then Python resolved this problem. By installing MacVim before installing Python it "linked" against the OS X Python interpreter.

:py import os
:py print(os.__file__)
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc

Previously this would print /usr/local/Cellar/python/2.7.3/lib/python2.7/os.pyc. So maybe there is some issue with MacVim and Homebrew's Python.

Nathaniel Nutter nnutter closed this
danielsdesk

I am having this same problem as well, though from within MacVim and in shell, the same Python is being linked... I was just using the OSX system installed Python so I didn't have it linked to the wrong Python (wasn't using Homebrew)... if I print(os.file) from inside of Vim and from just the python interpreter in term, it's the same path?

Lx Yu

The same problem, and find a great discussion here: http://goo.gl/4FuHx

And the solution is "it is best to avoid having two different python minor versions on the same system."

So this works for me.

$ brew remove python
Luke Orland

The sequence of commands that fixed this for me was:

brew unlink python
brew unlink macvim
brew remove macvim
brew install -v --force macvim
brew link macvim
brew link python
Aaron O'Leary

I got the same maddening bug on a centos system that I don't have root / superuser access to.

This gave me some clues as to the problem. I used Enthought 7.3-1, with python version 2.7.3 (system default is 2.4). In vim :py import sys; print sys.version gave 2.7.2.

The problem arises because the way to import urandom was changed between 2.7.2 and 2.7.3.

Solution

I 'solved' the problem by setting up Enthought 7.2-1 (with python 2.7.2), and compiling vim 7.3 (system default is 7.0) with

./configure --with-features=big --prefix=$HOME --enable-pythoninterp \
            --with-python-config-dir=/apps/enthought-7.2-1/lib/python2.7/config

and at the top of .vimrc

let $PYTHONHOME='/apps/enthought-7.2-1/'

Caveat

I say 'solved' because I could not find a way to get vim to use python 2.7.3. If I changed the config-dir above to 7.3-1, I get the following on configuration:

checking for python... (cached) /apps/enthought-7.2-1/bin/python
checking Python version... (cached) 2.7
checking Python is 1.4 or better... yep
checking Python's install prefix... (cached) /apps/enthought-7.2-1
checking Python's execution prefix... (cached) /apps/enthought-7.2-1
(cached) checking Python's configuration directory... (cached) /apps/enthought-7.3-1/lib/python2.7/config

whereas with 7.2-1

checking for python... (cached) /apps/enthought-7.2-1/bin/python
checking Python version... (cached) 2.7
checking Python is 1.4 or better... yep
checking Python's install prefix... (cached) /apps/enthought-7.2-1
checking Python's execution prefix... (cached) /apps/enthought-7.2-1
(cached) checking Python's configuration directory... (cached) /apps/enthought-7.2-1/lib/python2.7/config

Then I gave up. It works with python 2.7.2.

Edward Liaw

There's a problem with the configure script that tells the makefile to use the built-in python framework (2.7.1 by default on OSX 10.7). You can build VIM using a different version of python 2.7 (if it's been built into a framework: see http://svn.python.org/projects/python/trunk/Mac/README or use macports/homebrew to install it) by changing this line in the configure script (.../src/auto/configure) before you build it:
vi_cv_path_python_plibs="-framework Python"
to
vi_cv_path_python_plibs="-F/opt/local/Library/Frameworks -framework Python"
or wherever your framework is located (above is where MacPorts puts it). Be sure to make distclean and reconfigure before making again.

You may also need to set your CFLAGS and LDFLAGS to the correct libraries for your python installation.
i.e., in LDFLAGS include:
-L/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib
and in CFLAGS include:
-I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include -I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Headers

snoby

I am not 100% clear on resolving this is issue. I have just installed the binary install of python 2.7.3 and I have installed the binary install of macvim 7.3.646. I also have installed python-mode version 0.69 and I'm still getting the urandom error when starting the terminal version of mvim. I really don't want to have to recompile macvim. Is this the only way to resolve this error? Thanks.

Edward Liaw

I think the issue arises when it links to one version of the python interpreter, but uses the config directory of another version. More specifically, 2.7.<3 and 2.7.3 because of the change to urandom (see aaren's post). To fix it, you will have to recompile macvim or use a precompiled version of macvim I'm sure.

Aaron O'Leary

I was forced to look at this again recently as I can't use python 2.7.2 anymore. The fix is quite simple really (if you're configuring your own Vim). All that stuff in the output from configure points towards something being cached. If you look at the configure script, it eventually points towards src/auto/config.cache in which I found (specific to my case) lots of references to python 2.7.2. Removing this file and recompiling, specifying python 2.7.3, fixed it for me.

NB: make clean doesn't remove configuration output (including the config.cache file) - this is what make distclean does. On your system this solution may have the same effect as edilaw's solution above, but it may be that you have to follow the specific steps of that solution.

Summary

If you're compiling your own Vim:

make distclean
./configure --with-features=big --prefix=$HOME --enable-pythoninterp \
            --with-python-config-dir=/path/to/your/python/lib/python2.7/config

At the top of your vimrc add

let $PYTHONHOME='/path/to/your/python/'

This isn't really an issue with Ctrl-P, rather with the compilation of Vim with Python support.

Evan Wheeler

I had some similar issues on Mac OS 10.6 where python-mode used the system-installed python instead of my homebrew-installed python.
Reinstalling macvim with these changes fixed everything for me: http://superuser.com/a/440982

Hari Jayaram

I had the same issue when I tried to compile with the brew installed python. In the end as other commenters pointed out I had to install vim against the Apple supplied python .Briefly:

I had to not use the homebrew installed python on OSX , but then build vim using the homebrew recipe here (https://gist.github.com/721952)
So Briefly
1) brew unlink python
2) brew remove mercurial ( in case it was built against the brew installed python)
3) brew install mercurial
4) brew install https://gist.github.com/raw/721952/843b390acb9f86226133ce3e94ed99ee485c494f/vim.rb --HEAD --enable-interp=python --with-features=huge
Then after that I stopped getting the pymode errors , even after I linked back the brew python

Sebastian Dahlgren

The commands @lukeorland suggested worked fine for me on Mac OS X.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.