Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name urandom #87

Closed
nnutter opened this issue Apr 29, 2012 · 13 comments
Closed

ImportError: cannot import name urandom #87

nnutter opened this issue Apr 29, 2012 · 13 comments

Comments

@nnutter
Copy link

nnutter commented Apr 29, 2012

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.

@klen
Copy link
Collaborator

klen commented Apr 30, 2012

Maybe others OSX users can be help.

@nnutter
Copy link
Author

nnutter commented May 10, 2012

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.

@nnutter nnutter closed this as completed May 10, 2012
@danielsdesk
Copy link

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?

@lxyu
Copy link

lxyu commented Sep 3, 2012

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

@lukeorland
Copy link

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

@aaren
Copy link

aaren commented Oct 18, 2012

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.

@edliaw
Copy link

edliaw commented Oct 18, 2012

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
Copy link

snoby commented Oct 23, 2012

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.

@edliaw
Copy link

edliaw commented Oct 23, 2012

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.

@aaren
Copy link

aaren commented Nov 1, 2012

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.

@ewheeler
Copy link

ewheeler commented Nov 6, 2012

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

@harijay
Copy link

harijay commented Nov 7, 2012

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

@sebdah
Copy link

sebdah commented Aug 27, 2013

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

teddywing added a commit to teddywing/vim-space-vlaze that referenced this issue Oct 15, 2015
Ostensibly this should work and generate us a random number using Python
without having to call `system`. My hope is that this call doesn't block
rendering and execution of our game.

The trouble is that I can't get it to work because I have Python 2.7.1
installed as my system Python (which Vim is linked against), and Python
2.7.9 installed via Homebrew. Vim is looking in the Homebrew path when
it should be looking in the system path to Python and causing this
error:

    Error detected while processing function space_vlaze#random#Random:
    line    7:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 47, in <module>
        from os import urandom as _urandom
    ImportError: cannot import name urandom

More information:
- python-mode/python-mode#87

We could get around this by not having these 2 versions of Python
installed on our system (the random implementation changed between 2.7.2
and 2.7.3) or by setting `$PYTHONHOME` in our vimrc.

Could, but I'm sick of this shite. Would have liked to be able to use
Python for this because in my opinion Python tends to be pretty fast as
interpreted languages go, but frak it I'll just use Ruby.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests