Python2.6 cannot import urandom - python

I was working on an old Django project (Django version 1.3.2, Python version 2.6.6). Running "python manage.py runserver" gives me: "ImportError: cannot import name urandom".
Searching online returns a bunch of results about solving the problem by re-running virtualenv. This doesn't make sense because I never had virtualenv installed and things worked before...so I figured it must be a problem with my python installation.
I decided to remove and re-install Django by following the Django installation guide by running
pip install Django
Doing django-admin.py startproject project gives me the same ImportError. So I uninstalled Django again using pip uninstall Django.
Now running python starts the python2.6.6 shell, typing from os import urandom gives me the "ImportError: cannot import name urandom" message, while doing the same in python2.7 does not give such error.
What's going on here? What's breaking my python2.6.6?

While I don't know if I can provide any more info that the many helpful comments available, here is my stab at the possible reasons for this failure:
Your OS is no longer providing urandom properly to Python 2.6.6. From what I can tell here OSError with "import random" when /dev/urandom doesn't exist, this should result in an OSError or NotImplementedError (rather than an ImportError) but docs have been wrong before. A decent way to check if you can get to the system's urandom is to run:
from posix import urandom
If that doesn't work, try:
from os import O_RDONLY
aURandom = open("/dev/urandom", O_RDONLY)
If neither work, you're probably dealing with an inability to get to the system function by any means. That probably means OS setup is somehow to blame. You might want to actually check if urandom is actually available on your Ubuntu installation where python expects it (/dev/urandom). Your version of Python 2.7 might also be failing to access urandom too, but failing silently instead of crashing.
If the above snippet of code works, then your os.py file or a compiled version of such is messed up. In that case, your Python installation is broken. You'd need to uninstall it, clear out any intermediate pyc or pyo files, then re-install it.
Given that the python 2.6.6 interpreter can't find urandom on its own, Django is unlikely to be the issue.

Related

Importing python modules works in command line but not in the python 3.8 shell

I'm having a problem with importing modules in python.
When I run my program in the command line it works perfectly fine.
However, when I try to run the same program in the python shell I am prompted with the following error:
ModuleNotFoundError: No module named 'matplotlib'
I already successfully installed matplotlib using 'python -m pip install matplotlib'.
I've read this can happen when you have two different versions of python installed; however, I don't.
I've uninstalled and reinstalled python and I still am having the same issue. I've also uninstalled and reinstalled matplotlib using pip.
I believe my problem is the module paths that python uses to search for imported modules are different between the two.
When I use the 'print(sys.path)' command in the python shell and the command line I get two different outputs.
Any help would be greatly appreciated!!!
The file different system paths between the python shell and the command line
You have two versions of python. I would recommend you to remove all pythons you have and go for anaconda https://www.anaconda.com/distribution/. It will fix your path problems and allow you to create environments with different versions of python. This is the least painful way also for future :) good luck.
I suppose, you have both of the Python versions installed on the same computer.
If that is so, then my answer would be to go inside both Python script folders and install matplotlib on both of them.
I have also faced that issue. My path includes pip of Python 3.7.1 and whenevwer I try to import modules on Python 3.4. It throws an error!
Maybe, you could add both of the Pythons to the path.
I encountered this same problem – python -c "import sklearn" would work just fine, but import sklearn inside a Python program failed. Both my one-liner and program was using the same Python version (version 3.8.10).
I eventually got the program to work by replacing the shebang line (originally #!/usr/bin/python) with #!/bin/env python.
I don't know why this worked exactly (sorry). Presumably some path got reset, and the module loaded from a different location, but it might help someone so I'm posting it here nontheless. (If you know more, feel free to edit this answer.)

how to fix the "__path__ attribute not found" error for packages installed by pip installer?

I recently installed the opencv package using pip install and I wrote a small code to test it (cvtest.py). The code runs through the python idle shell but running it though the command prompt gives the error
Error while finding module specification for 'cvtest.py' (ModuleNotFoundError: __path__ attribute not found on 'cvtest' while trying to find 'cvtest.py')
I tried uninstalling and reinstalling both python and the package. looking up the system path using python -m site gives these results. I am the only user of my laptop.
sys.path = [
'C:\\Users\\Kareem Mostafa\\Desktop\\Assignments\\computer vision',
'G:\\Python37\\python37.zip',
'G:\\Python37\\DLLs',
'G:\\Python37\\lib',
'G:\\Python37',
'G:\\Python37\\lib\\site-packages',
This is the code I am using
import cv2
x=cv2.imread('backpack for sale.jpg',0)
cv2.imshow('x',x)
update: the problem is happening with all the py files I am having whether they require imports or not. apparently python is looking for _init_.py for all the files as if they are packages. Any idea what is going on?
For anyone else that had this problem (assuming kareemostafa has fixed it now!)
Removing the .py suffix on the python -m command fixes this problem, it appears -m only requires module names whereas running it directly as a python file (no -m option) requires the .py suffix
In your case python -m cvtest should be sufficient.

Cannot load Python bindings for MySQL - Trac

I recently started getting the following error in my trac install:
Cannot load Python bindings for MySQL
I, unfortunately, had to reinstall MySQL, and, afterwards, I started getting those issues. I have spent the last hour trying to track something down on Google, but I couldn't get anything to work. I made sure MySQL is up and running correctly. I created a new MySQL user specific to trac. I made sure all of the packages are there there. Nothing.
Here's my dkpg (I run Ubuntu Maverick):
dpkg -l | grep python
libapache2-mod-python
libapache2-mod-python-doc
libpython2.6
libpython3.1
python
python-central
python-docutils
python-genshi
python-imaging
python-lxml
python-minimal
python-mysqldb
python-pkg-resources
python-pybabel
python-pygments
python-roman
python-setuptools
python-subversion
python-support
python-tz
python2.6
python2.6-minimal
python3.1
python3.1-minimal
If anyone has any ideas as to what it could be, I'd be forever grateful.
That error should result when Trac tries to do "import MySQLdb" but it fails. You do have the right package in place which should provide that module, but for some reason it's failing. To find out why, you should look at the environment in which Trac is running. Is it using a Python other than /usr/bin/python2.6? Does it have a $PYTHONPATH set?
You may also want to try importing MySQLdb directly:
/usr/bin/python2.6 -c 'import MySQLdb'
That should have no output if the package can be loaded as expected.
Lastly, this may not be related, but you know that Maverick is no longer supported, right? It won't get any security updates or other support from Ubuntu.
Edit:
The python2.6 import worked fine, so we need to look closer at the trac environment. One way to do this will be to edit the db/mysql_backend.py file and insert some debugging information. (If this is an OS-packaged install, you should find that file under /usr/lib/python2.6/dist-packages/trac/; otherwise, you probably know where you put it). Let's try changing the MySQLConnector.get_supported_schemes() method, since I'm not sure where plain prints will show up for you.
def get_supported_schemes(self):
if not has_mysqldb:
import sys
self.error = "Cannot load Python bindings for MySQL. sys.path = %r, sys.executable = %r" \
% (sys.path, sys.executable)
yield ('mysql', -1 if self.error else 1)
instead of calling a specific python install like the paul is suggesting, try this at your terminal:
launch the path's python instance
python
see if you can import mysqldb
import MySQLdb
also run for us a:
pip freeze
we should see a mysql-python in the output

Running django-admin.py startproject is giving me an error

I've just started looking into python and django.. Im pretty sure ive successfull installed both python and the django framework and i managed to add python and django-admin.py to my system path but now when i run the command django-admin.py startproject My_Test_Site, in the folder i want to create a new project directory in (just following the tutorials), i get the following error:
What am i doing wrong?
This is not a Django error. It appears it cannot find the Python module unicodedata which is part of the Python Standard Library (docs). I see that there is also a bug report about this issue (Python 2.7 on Windows, see here). On the other hand, some people there report that reinstalling their Python installation fixed the problem. You could give that a try.
notice the last line, it says import unicodedata, importError
im guessing you probably didnt install your django properly.
so reinstall it and check that django is in your "path", i believe django does this automatically when it installs but doesnt hurt to double check.
if reinstalling django still doesn't work, i suggest you uninstall python and django related stuff.
then install python, install pip, then use pip install django.

ImportError exception encountered while trying to use Fabric

I am using Ubuntu and virtualenv, and I am having this recurring problem, while attempting to use Fabric to create a deployment script. Fabric depends on paramiko, which depends on PyCrypto.
Each time I try to use Fabric, or PyCrypto directly, I get this error:
ImportError: cannot import name Random
I have tried reinstalling with pip install -U PyCrypto. I have also tried installing the python-crypto and python-crypto-dbg packages with Aptitude, to no avail. I still get the same error. Anyone have any ideas that might help me solve this problem? Thanks in advance!
It's possible that there's a file name collision in your the directory from which you're running Fabric. Do you have a file called Crypto.py in your project?
Can you get Crypto.Random to import from outside of your project directory? (but still using your virtualenv. Ipython is a big help here.)
I've had the same trouble in the past using Ubuntu. I no longer have a Ubuntu install available, but on my old 10.04 box, the file Random in the Crypto directory was missing. Make sure it exists, if it doesn't, that's the problem.

Categories