python "ImportError: cannot import name urandom" - python

Somehow my python is broken and emits the error:
jseidel#EDP15:/etc/default$ python -c 'import random'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.6/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
This is NOT the virtualenv error that is so commonly documented here and elsewhere: I don't use python directly, I have never setup a virtualenv explicitly, and there is no virtualenv directory or python script that I can find anywhere on my system.
I'm running Kubuntu 10.04 and until just recently my KPackageKit worked just fine and handled updates with no problem. Now it shows nothing... maybe because of this python error, maybe because of something else.
How do I go about finding the error and fixing python?

As suggested by #Armin Rigo, this worked for me:
1) Add a print 42 at the end of the /usr/lib/python2.6/os.py file.
2) If you see "42", then that is the correct os.py file and the urandom module is not include. Add the statement to include urandom (you can find a sample from another os.py file). This was what worked for me.
3) If you don't see "42", then that's not the os.py file that you're using. Find the random.py file that is crashing and insert import os; print os.__file__ to get more information about the failure.

Related

ModuleNotFoundError: No module named 'bayesnet'

I'm trying to use the OpenBayes module, but the problems start from the very first step :'(
When I try to import from OpenBayes
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Мари\AppData\Local\Programs\Python\Python36\lib\site-packages\OpenBayes\__init__.py", line 7, in <module>
from bayesnet import *
ModuleNotFoundError: No module named 'bayesnet'
UPD: While installing (from .exe file) for py2 i gor error: "could not set a key value" (not a python error, but in dialog window)
I tried using pip install from console, but still get errors there.
Command "python setup.py egg_info" failed with error code 1 in C:\Users\CD3B~1\AppData\Local\Temp\pip-build-m4nnwa4o\OpenBayes\
Also not sure which py (2 or 3) is used when i type a command from console(
(Sorry for all that stupis questions)
You should
from OpenBayes import *
or
from OpenBayes import BNet
Here is an example demonstrating it's usage:
https://github.com/willasaywhat/OpenBayes-Fork/blob/master/Examples/bn_asia.py
I had the same problem and I created a directory with the same name as the root folder (within the root folder). Python then referred Its import calls to the src directory and not the folder. I also changed the python interpreter to a virtual (anaconda.exe), as the python install did not contain many of the modules that were being called by the application (flask, etc)
It's a weird workaround, but it worked for me. Hope this helps

Tweepy: ImportError: cannot import name Random

I'm using Tweepy to send some messages to Twitter. I'm getting a long traceback from the API. The traceback settles at:
ImportError: cannot import name Random
I used Pip to install the latest version of Tweepy:
Name: tweepy
Version: 2.3.0
Location: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Requires:
When I call
import tweepy
I get this traceback:
Traceback (most recent call last):
File "/Users/dromard/Projects/Drop Playlist/drop.py", line 4, in <module>
import tweepy
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/__init__.py", line 14, in <module>
from tweepy.api import API
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/api.py", line 8, in <module>
from tweepy.binder import bind_api
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/binder.py", line 5, in <module>
import httplib
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 79, in <module>
import mimetools
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 35, in <module>
from random import Random as _Random
ImportError: cannot import name Random
Process finished with exit code 1
I'm working in PyCharm and confirmed the site package and Python paths are correct in settings. I manually checked in console that the paths are correct, and there are no duplicate locations.
I haven't made any changes to Tweepy. I let Pip install it where it is, as it is. Permissions look correct:
-rw-r--r-- 1 root wheel
If I check Python in console:
- I get the same traceback
- When I run the individual imports, they all execute without error
It all fails out at the random call. I think random is part of Python's core packages, and not part of Tweepy.
I handed this script off to a co-worker, who then used Pip to install tweepy and hit the same traceback. Makes me think Pip might be contributing.
I'm relatively new to Python (programming in general). I looked through other 'import error' articles, but didn't find this specific issue. Any help is appreciated.
I figured this out.
I had created a python file called 'random.py' during the course of experimenting with a random number generating script. My 'import random' call was grabbing this file, which lacked the library Random. It essentially created a conflict with the proper 'random.'
Make sure that you delete the random.py file or overwrite it with another name, then recheck "from random import random".
I could not comment due to low experience.
I also had a xyz.py in a directory and calling a module of same name (import xyz) causes this error.
Make sure you do not save any file xyz.py that also have a python module at the same name.
This is what I found from my newbie experiment.
This also happens with python 3.8.

Raspberry Pi Python Program First Line Error/Python Library Error

The first two lines of the program are:
from i2clibraries import i2c_lcd
from ABElectronics_ADCPi import ADCPi
No matter what line is first the Pi returns an error when I attempt to run it under Python or Python 3. All the libraries are possessed and registered. Using the shell commands the checks saying the exports worked correctly all show up correctly. However, whatever line is line 1 will return a missing module error and the i2clibraries will always return a missing module error. By keeping that as the first line I get the least errors in running, but the program still returns this:
pi#raspberrypi ~ $ sudo python file.py
Traceback (most recent call last):
File "file.py", line 1, in <module>
from i2clibraries import i2c_lcd
File "/home/pi/i2clibraries/i2c_lcd.py", line 1, in <module>
from i2clibraries import i2c
File "/home/pi/i2clibraries/i2c.py", line 1, in <module>
from quick2wire.i2c import I2CMaster, writing_bytes, reading
ImportError: No module named quick2wire.i2c
Given the error, what possible solutions are there to stop the first line from being unable to find its module?
Problem
The error message is telling you that when you try to import the i2clibraries module, the imports that it requires (dependencies) cannot be found when it is itself is being imported. This is specifically in the first line of i2c.py file - where the line
from quick2wire.i2c import I2CMaster, writing_bytes, reading
is failing.
The problem is almost certainly that your modules are not on the Python module search path. Further info on this is given at the end of this answer should you need it.
Solution
There are a number of ways to resolve this. The one recommended by the developers of the module is
To use the library without installation, add the full path of the
source tree to the PYTHONPATH environment variable. For example:
export QUICK2WIRE_API_HOME=[the directory cloned from Git or unpacked from the source archive]
export PYTHONPATH=$PYTHONPATH:$QUICK2WIRE_API_HOME
So, you need to know where your quick2wire libraries are installed - from your error message, I would hazard a guess that they are in /home/pi/i2clibraries/, so $QUICK2WIRE_API_HOME=/home/pi/i2clibraries/ should be your first line of the above pair.
Further info
You can read more generally about how to install modules on Python 2.x on the Python website. You can look at what paths make up the module search path by going to an interactive Python prompt (i.e. typing python) and then doing.
>>> import sys
>>> sys.path
This will output a list containing strings representing all the paths that will be searched for modules.

Why do I receive an ImportError when running one of the CherryPy tutorials

I have installed CherryPy 3.1.0,. Here is what happens when I try to run tutorial 9:
$ cd /Library/Python/2.5/site-packages/cherrypy/tutorial/
$ python tut09_files.py
Traceback (most recent call last):
File "tut09_files.py", line 48, in <module>
from cherrypy.lib import static
ImportError: cannot import name static
The previous line in the file:
import cherrypy
passes without error, so it appears that it can find cherrypy on the path. What am I missing?
This works for me, and I'm also using CherryPy 3.1.0, so I'm not sure what to tell you.
Look in your /Library/Python/2.5/site-packages/cherrypy/lib directory for a file named static.py; if this file exists then I'm not sure what to tell you. If it doesn't then something has happened to your CherryPy and I'd advise you to reinstall. If it does then you should check the value of sys.path to make sure it's detecting the right version of CherryPy.
You can also try running the python interpreter on the command line and then doing a from cherrypy.lib import static to see if you get the same result.
I had an old CherryPy-2.3.0-py2.5.egg file in my site-packages. After removing the old .egg I could run the tutorial.

from twisted.words.protocols import irc no module named words.protocols

I am trying to create a simple irc client with twisted library and start by importing related module:
from twisted.words.protocols import irc
This error is displayed:
no module named words.protocols
I don't quite understand this error as I can import tiwsted directly (it is in my PATH then) and the filesystem is like this:
/usr/local/lib/python2.7/dist-packages/twisted/words/protocols/irc.py
The words and protocols folders both contain a __init__.py file, so they should be interpreted as modules.
If I change directory for /usr/local/lib/python2.7/dist-packages/twisted/words/ and start a python interpreter, this command runs without error:
from protocols import irc
But my final script would have nothing to do in this directory :/
I don't quite understand why I'm having this error and how to solve it.
Many thanks for helping
EDIT
I have all the words folders into /usr/local/lib and not in classic /usr/lib, maybe python is to look into the second folder and not the first one.
I don't know yet how to overload this behavior, and hope it does not boils down to overriding os.environ['PATH'] which looks really dirty.
EDIT2: /usr/local/lib folders belong to the PATH as shown by this command:
python -c "import sys; print [ i for i in sys.path if 'local' in i ]"
['/usr/local/lib/python2.7/dist-packages']
So I still don't understand why this shit is not going to be imported
EDIT3
This is not script name related I guess; from a terminal in my debian :
python -c "from twisted.words.protocols import irc"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named words.protocols

Categories

Resources