Cannot load Python bindings for MySQL - Trac - python

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

Related

No module named 'stix2' [duplicate]

After installing mechanize, I don't seem to be able to import it.
I have tried installing from pip, easy_install, and via python setup.py install from this repo: https://github.com/abielr/mechanize. All of this to no avail, as each time I enter my Python interactive I get:
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
>>>
The installations I ran previously reported that they had completed successfully, so I expect the import to work. What could be causing this error?
In my case, it is permission problem. The package was somehow installed with root rw permission only, other user just cannot rw to it!
I had the same problem: script with import colorama was throwing an ImportError, but sudo pip install colorama was telling me "package already installed".
My fix: run pip without sudo: pip install colorama. Then pip agreed it needed to be installed, installed it, and my script ran. Or even better, use python -m pip install <package>. The benefit of this is, since you are executing the specific version of python that you want the package in, pip will unequivocally install the package into the "right" python. Again, don't use sudo in this case... then you get the package in the right place, but possibly with (unwanted) root permissions.
My environment is Ubuntu 14.04 32-bit; I think I saw this before and after I activated my virtualenv.
I was able to correct this issue with a combined approach. First, I followed Chris' advice, opened a command line and typed 'pip show packagename'
This provided the location of the installed package.
Next, I opened python and typed 'import sys', then 'sys.path' to show where my python searches for any packages I import. Alas, the location shown in the first step was NOT in the list.
Final step, I typed 'sys.path.append('package_location_seen_in_step_1'). You optionally can repeat step two to see the location is now in the list.
Test step, try to import the package again... it works.
The downside? It is temporary, and you need to add it to the list each time.
It's the python path problem.
In my case, I have python installed in:
/Library/Frameworks/Python.framework/Versions/2.6/bin/python,
and there is no site-packages directory within the python2.6.
The package(SOAPpy) I installed by pip is located
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
And site-package is not in the python path, all I did is add site-packages to PYTHONPATH permanently.
Open up Terminal
Type open .bash_profile
In the text file that pops up, add this line at the end:
export PYTHONPATH=$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
Save the file, restart the Terminal, and you're done
The Python import mechanism works, really, so, either:
Your PYTHONPATH is wrong,
Your library is not installed where you think it is
You have another library with the same name masking this one
I have been banging my head against my monitor on this until a young-hip intern told me the secret is to "python setup.py install" inside the module directory.
For some reason, running the setup from there makes it just work.
To be clear, if your module's name is "foo":
[burnc7 (2016-06-21 15:28:49) git]# ls -l
total 1
drwxr-xr-x 7 root root 118 Jun 21 15:22 foo
[burnc7 (2016-06-21 15:28:51) git]# cd foo
[burnc7 (2016-06-21 15:28:53) foo]# ls -l
total 2
drwxr-xr-x 2 root root 93 Jun 21 15:23 foo
-rw-r--r-- 1 root root 416 May 31 12:26 setup.py
[burnc7 (2016-06-21 15:28:54) foo]# python setup.py install
<--snip-->
If you try to run setup.py from any other directory by calling out its path, you end up with a borked install.
DOES NOT WORK:
python /root/foo/setup.py install
DOES WORK:
cd /root/foo
python setup.py install
I encountered this while trying to use keyring which I installed via sudo pip install keyring. As mentioned in the other answers, it's a permissions issue in my case.
What worked for me:
Uninstalled keyring:
sudo pip uninstall keyring
I used sudo's -H option and reinstalled keyring:
sudo -H pip install keyring
In PyCharm, I fixed this issue by changing the project interpreter path.
File -> Settings -> Project -> Project Interpreter
File -> Invalidate Caches… may be required afterwards.
I couldn't get my PYTHONPATH to work properly. I realized adding export fixed the issue:
(did work)
export PYTHONPATH=$PYTHONPATH:~/test/site-packages
vs.
(did not work)
PYTHONPATH=$PYTHONPATH:~/test/site-packages
This problem can also occur with a relocated virtual environment (venv).
I had a project with a venv set up inside the root directory. Later I created a new user and decided to move the project to this user. Instead of moving only the source files and installing the dependencies freshly, I moved the entire project along with the venv folder to the new user.
After that, the dependencies that I installed were getting added to the global site-packages folder instead of the one inside the venv, so the code running inside this env was not able to access those dependencies.
To solve this problem, just remove the venv folder and recreate it again, like so:
$ deactivate
$ rm -rf venv
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
Something that worked for me was:
python -m pip install -user {package name}
The command does not require sudo. This was tested on OSX Mojave.
In my case I had run pip install Django==1.11 and it would not import from the python interpreter.
Browsing through pip's commands I found pip show which looked like this:
> pip show Django
Name: Django
Version: 1.11
...
Location: /usr/lib/python3.4/site-packages
...
Notice the location says '3.4'. I found that the python-command was linked to python2.7
/usr/bin> ls -l python
lrwxrwxrwx 1 root root 9 Mar 14 15:48 python -> python2.7
Right next to that I found a link called python3 so I used that. You could also change the link to python3.4. That would fix it, too.
In my case it was a problem with a missing init.py file in the module, that I wanted to import in a Python 2.7 environment.
Python 3.3+ has Implicit Namespace Packages that allow it to create a packages without an init.py file.
Had this problem too.. the package was installed on Python 3.8.0 but VS Code was running my script using an older version (3.4)
fix in terminal:
py .py
Make sure you're installing the package on the right Python Version
I had colorama installed via pip and I was getting "ImportError: No module named colorama"
So I searched with "find", found the absolute path and added it in the script like this:
import sys
sys.path.append("/usr/local/lib/python3.8/dist-packages/")
import colorama
And it worked.
I had just the same problem, and updating setuptools helped:
python3 -m pip install --upgrade pip setuptools wheel
After that, reinstall the package, and it should work fine :)
The thing is, the package is built incorrectly if setuptools is old.
If the other answers mentioned do not work for you, try deleting your pip cache and reinstalling the package. My machine runs Ubuntu14.04 and it was located under ~/.cache/pip. Deleting this folder did the trick for me.
Also, make sure that you do not confuse pip3 with pip. What I found was that package installed with pip was not working with python3 and vice-versa.
I had similar problem (on Windows) and the root cause in my case was ANTIVIRUS software! It has "Auto-Containment" feature, that wraps running process with some kind of a virtual machine.
Symptoms are: pip install somemodule works fine in one cmd-line window and import somemodule fails when executed from another process with the error
ModuleNotFoundError: No module named 'somemodule'
In my case (an Ubuntu 20.04 VM on WIN10 Host), I have a disordered situation with many version of Python installed and variuos point of Shared Library (installed with pip in many points of the File System). I'm referring to 3.8.10 Python version.
After many tests, I've found a suggestion searching with google (but' I'm sorry, I haven't the link). This is what I've done to resolve the problem :
From shell session on Ubuntu 20.04 VM, (inside the Home, in my case /home/hduser), I've started a Jupyter Notebook session with the command "jupyter notebook".
Then, when jupyter was running I've opened a .ipynb file to give commands.
First : pip list --> give me the list of packages installed, and, sympy
wasn't present (although I had installed it with "sudo pip install sympy"
command.
Last with the command !pip3 install sympy (inside jupyter notebook
session) I've solved the problem, here the screen-shot :
Now, with !pip list the package "sympy" is present, and working :
In my case, I assumed a package was installed because it showed up in the output of pip freeze. However, just the site-packages/*.dist-info folder is enough for pip to list it as installed despite missing the actual package contents (perhaps from an accidental deletion). This happens even when all the path settings are correct, and if you try pip install <pkg> it will say "requirement already satisfied".
The solution is to manually remove the dist-info folder so that pip realizes the package contents are missing. Then, doing a fresh install should re-populate anything that was accidentally removed
When you install via easy_install or pip, is it completing successfully? What is the full output? Which python installation are you using? You may need to use sudo before your installation command, if you are installing modules to a system directory (if you are using the system python installation, perhaps). There's not a lot of useful information in your question to go off of, but some tools that will probably help include:
echo $PYTHONPATH and/or echo $PATH: when importing modules, Python searches one of these environment variables (lists of directories, : delimited) for the module you want. Importing problems are often due to the right directory being absent from these lists
which python, which pip, or which easy_install: these will tell you the location of each executable. It may help to know.
Use virtualenv, like #JesseBriggs suggests. It works very well with pip to help you isolate and manage the modules and environment for separate Python projects.
I had this exact problem, but none of the answers above worked. It drove me crazy until I noticed that sys.path was different after I had imported from the parent project. It turned out that I had used importlib to write a little function in order to import a file not in the project hierarchy. Bad idea: I forgot that I had done this. Even worse, the import process mucked with the sys.path--and left it that way. Very bad idea.
The solution was to stop that, and simply put the file I needed to import into the project. Another approach would have been to put the file into its own project, as it needs to be rebuilt from time to time, and the rebuild may or may not coincide with the rebuild of the main project.
I had this problem with 2.7 and 3.5 installed on my system trying to test a telegram bot with Python-Telegram-Bot.
I couldn't get it to work after installing with pip and pip3, with sudo or without. I always got:
Traceback (most recent call last):
File "telegram.py", line 2, in <module>
from telegram.ext import Updater
File "$USER/telegram.py", line 2, in <module>
from telegram.ext import Updater
ImportError: No module named 'telegram.ext'; 'telegram' is not a package
Reading the error message correctly tells me that python is looking in the current directory for a telegram.py. And right, I had a script lying there called telegram.py and this was loaded by python when I called import.
Conclusion, make sure you don't have any package.py in your current working dir when trying to import. (And read error message thoroughly).
I had a similar problem using Django. In my case, I could import the module from the Django shell, but not from a .py which imported the module.
The problem was that I was running the Django server (therefore, executing the .py) from a different virtualenv from which the module had been installed.
Instead, the shell instance was being run in the correct virtualenv. Hence, why it worked.
This Works!!!
This often happens when module is installed to an older version of python or another directory, no worries as solution is simple.
- import module from directory in which module is installed.
You can do this by first importing the python sys module then importing from the path in which the module is installed
import sys
sys.path.append("directory in which module is installed")
import <module_name>
Most of the possible cases have been already covered in solutions, just sharing my case, it happened to me that I installed a package in one environment (e.g. X) and I was importing the package in another environment (e.g. Y). So, always make sure that you're importing the package from the environment in which you installed the package.
For me it was ensuring the version of the module aligned with the version of Python I was using.. I built the image on a box with Python 3.6 and then injected into a Docker image that happened to have 3.7 installed, and then banging my head when Python was telling me the module wasn't installed...
36m for Python 3.6
bsonnumpy.cpython-36m-x86_64-linux-gnu.so
37m for Python 3.7 bsonnumpy.cpython-37m-x86_64-linux-gnu.so
I know this is a super old post but for me, I had an issue with a 32 bit python and 64 bit python installed. Once I uninstalled the 32 bit python, everything worked as it should.
I have solved my issue that same libraries were working fine in one project(A) but importing those same libraries in another project(B) caused error. I am using Pycharm as IDE at Windows OS.
So, after trying many potential solutions and failing to solve the issue, I did these two things (deleted "Venv" folder, and reconfigured interpreter):
1-In project(B), there was a folder named("venv"), located in External Libraries/. I deleted that folder.
2-Step 1 (deleting "venv" folder) causes error in Python Interpreter Configuration, and
there is a message shown at top of screen saying "Invalid python interpreter selected
for the project" and "configure python interpreter", select that link and it opens a
new window. There in "Project Interpreter" drop-down list, there is a Red colored line
showing previous invalid interpreter. Now, Open this list and select the Python
Interpreter(in my case, it is Python 3.7). Press "Apply" and "OK" at the bottom and you
are good to go.
Note: It was potentially the issue where Virtual Environment of my Project(B) was not recognizing the already installed and working libraries.

Verify thread-safety MySQLdb (Python) prior to Trac installation

I'm trying to install Trac manually for the first time. I don't want to use a one-click-installer like Bitmani, I want to learn how to install Trac manually, so I'm following the instructions carefully. I'm installing it in a Windows localhost for now, before installing it in a Linux environment.
As I follow the instructions carefully, I needed to install Python+MySQLDb, and I read this:
thread-safety is important
(...) verify that it is thread-safe by calling MySQLdb.thread_safe() from a standalone Python script (i.e., not under Apache). If the stand-alone test reports that MySQLdb is indeed thread-safe (...)
I've just installed MySQLDb 1.2.4 and I'd like to verify this. I've Googled but I haven't found an example about this, and I have no idea about Python. How can I verify if I've got a thread-safe installation?
Run this command. If you get 1 in the output, your installation is threadsafe.
python -c "import MySQLdb ; print MySQLdb.thread_safe()"

How can I update Python on Mac OS and have RubyGems use the new version?

I am having serious issues trying to download the "therubyracer" gem. I think the problem is that I do not have a working libv8 library to create the gem.
I think I do not have a working libv8 because my RubyGems keeps trying to use "Python 2.5.6", which is too old to gem install libv8. I tried countless ways to update the Python that RubyGems uses, however, I cannot figure it out.
Here is what I have done thus far. I went to http://www.python.org/download/releases/3.3.2/ and downloaded "(Mac OS X 64-bit/32-bit Installer (3.3.2) for Mac OS X 10.6 and later 2.)".
Upon completion of the download, I ran:
gem install libv8 --version 3.11.8.17
Yet, I still received the same error. I noticed the output still said:
"Using Python 2.5.4"
Despite downloading a newer Python version, it is not being used. So how do I change the version of Python that the "gem" command uses? I searched and discovered "virtualenv," which appears to be a good Python manager, however I am a bit at a dead end now. Any help or suggestion?
If libv8 uses the python command from the command line, this should apply:
If you do not care what python2 is installed, you can do this in the command line:
type python # this gives you the path of the python command
python is /usr/bin/python
type python2.7
python is /usr/bin/python2.7
Then you can copy one over the other
cp /usr/bin/python2.7 /usr/bin/python
You can safely copy python2.7 there because it is compatible with older versions.
Suggestions?

biopython test_Tutorial ... FAIL

I'm fairly new to programming. I'm trying to install biopython on mac os x 10.5.
This is what I did so far. 1. installed xcode 2. installed numpy 3. ran these commands in terminal python setup.py build python setup.py test
test reported back one fail.
test_Tutorial ... FAIL
ERROR: Run tutorial doctests.
Traceback (most recent call last): File "test_Tutorial.py", line 152, in test_doctests ValueError: 4 Tutorial doctests failed: test_from_line_05671, test_from_line_06030, test_from_line_06190, test_from_line_06479
Thanks for any help or advice.
That file test_Tutorial.py runs marked examples in the source for the main Biopython Tutorial and Cookbook ( http://biopython.org/DIST/docs/tutorial/Tutorial.html / http://biopython.org/DIST/docs/tutorial/Tutorial.pdf ) to check the examples work as expected. Internally this uses the same library as Python's doctest examples.
The fact that test_Tutorial.py failed is probably a harmless issue with a couple of examples.
Which version of Biopython are you using? If this was an official release that failure is unexpected. If it was a snapshot from the git repository then that is unfortunate. If you are curious, you could try this to see more information:
$ cd Tests
$ python test_Tutorial.py
This kind of issue might be more easily discussed on the Biopython mailing list http://biopython.org/wiki/Mailing_lists
I've found that the easiest way to set up a good programming environment is to use MacPorts, as it has a great way of ensuring that all dependencies are satisfied when installing things. You'll need to be familiar with Terminal and the command line, though.
Install MacPorts for OSX 10.5. Read the documentation too.
Restart.
Open Terminal.app and enter sudo port selfupdate to make sure the portfile definitions are up to date.
Run sudo port install py27-biopython to install the latest version of Python 2 (2.7.3), numpy, and biopython. This will take a while.
Run echo $PATH and make sure that /opt/local/bin and /opt/local/sbin` are at the beginning. They should be.
Run which python and ensure it returns /opt/local/bin/python. If it doesn't, run sudo port install python_select and follow its instructions to select your default python version.
Hopefully, at this point you can run python to enter the interactive interpreter, and import Bio won't give any errors.
Good luck!

Python2.6 cannot import urandom

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.

Categories