python random模块导入,Python:无法导入urandom模块(OS X)

I'm quite ashamed to ask a question like this one, but I've been trying for a couple of hours already...it seems I can't get my python version to do random things anymore. More precisely, it's missing the module urandom.

First, here are some info about my system:

OSX version: 10.7.4

python version: Python 2.7.1

which python: /opt/local/bin/python

import os; os:

echo $PATH: /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin

$ENV and $PYTHONPATH are empty

Now, when I have a deeper look and do vim /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py, I can see this file requires urandom:

line 47: from os import urandom as _urandom

So, quite logically, I have failures when I try running:

>>> import random

Traceback (most recent call last):

File "", line 1, in

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 47, in

from os import urandom as _urandom

ImportError: cannot import name urandom

Any ideas? Where is urandom supposed to live? I've been googling around without success so far.

It seems a lot of Ubuntu users had this problem with virtualenv, but I'm not using virtualenv, and I'm not on Ubuntu. My bet is that somehow my PATH or PYTHONPATH or ENV variables are not set properly, but I have no idea how to solve this.

解决方案

Ok, I figured it out.

I had a dirty hash table in my terminal.

Solution:

hash -r # will erase the currently used hash table

Once this was done, I ran python again and I got:

Python 2.7.3 (default, Apr 19 2012, 00:55:09)

[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import random

>>> import os; os

Note the different python version (2.7.3 vs 2.7.1 before). I think this is due to an earlier install of python 2.3 via mac port.

Sorry for the question/quick answer :/

Have a good night!

Arnaud

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值