Can't import MySQLdb module in Python - mysql

I'm trying to use MySQL in Python. I have installed the MySQL adapter (MySQL-python-1.2.4b4.win32-py2.7) for Python, but when I try to import the MySQLdb module (import MySQLdb) with command prompt I receive the following output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 19, in <module>
import _mysql
ImportError: DLL load failed: %1 is not a valid Win32 application.
Would anybody know, what could be wrong?

You need to have a 32bit version of Python to use this adapter. Which version are you actually using ?

I had problems installing the 64-bit version of MySQLdb on Windows via Pip, but if you want to continue using a 64-bit version of Python, rather than reverting to a 32-bit version, then you can install the compiled MySQLdb from the .whl file available from http://www.lfd.uci.edu/~gohlke/pythonlibs/
You will then want to install the .whl file as document in https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels

when you setup the module ,be careful the version ,MySQL-python-1.2.3.win32-py2.7.exe and MySQL-python-1.2.4.win32-py2.7.exe ,only one of the 2 version is you need,try 2 times

Related

PyModbusTCP module installation on windows pc

Last year, I made a program using the PyModbusTCP module on a raspberry Pi. Now I want to try my programs again on a windows pc but I can't seem to get my installations correct.
Here is some info:
project has a venv folder and a main.py next to it. I am not sure if the main.py has to be in the venv folder or not.
PyModbusTCP has been installed using git cmd with the command "pip3 install pyModbusTCP" and gives me a message "requirement already satisfied..."
Message in console after using a basic program provided by the pymodbustcp dev:
C:\Users\SESA681397\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\SESA681397\PycharmProjects\pythonProject\test.py
Traceback (most recent call last):
File "C:\Users\SESA681397\PycharmProjects\pythonProject\test.py", line 1, in <module>
from pyModbusTCP.client import ModbusClient
ModuleNotFoundError: No module named 'pyModbusTCP'
Thank you for your time reading this, it is probably a stupid problem but i don't know it.
I tried to get the folder correctly and see if pymodbustcp module is correctly installed.
Tried to see if the module is installed and if my previous programs worked.

from flask_mysqldb import MySQL

EDIT: Problem resolved. I was able to finally figure out the problem with some help from another student in the Udemy class along with the answer from Isaac_R here. I had to edit my .bash_profile with the following two lines of code and I am able to install MySQLdb and it is working as intended now. Here are the two lines I added to my .bash_profile
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
EDIT #2: I just started another mysql project in a new file, I had to run
pip3 install mysqlclient
before I could install mysqldb, just a heads up to someone who may have this issue like I did.
I am totally new to coding so please excuse my potential ignorance, I am trying my best to learn something new and it was going well until I ran into this issue that I am unable to get passed. I am on a Mac with 10.15.4 in python3.8.2 trying to create a MySQL database in order to use in a blog I am attempting to build. I have installed the mysql client along with pip3 install flask-mysqldb. I get successful conformation of the install, here is where my problems start. In Atom I am using the command from ''flask_mysqldb import MySQL'' to which I receive the error of the following
Traceback (most recent call last):
File "app.py", line 3, in <module>
from flask_mysqldb import MySQL
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask_mysqldb/__init__.py", line 1,
in <module>
import MySQLdb
File "IndentationError: unexpected indent
(venv) tylernelson#Tylers-MacBook-Air My Project % python3 app.py
Traceback (most recent call last):
File "app.py", line 3, in <module>
from flask_mysqldb import MySQL
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask_mysqldb/__init__.py", line 1,
in <module>
import MySQLdb
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <
module>
from . import _mysql
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-
38-darwin.so, 2): Library not loaded: #rpath/libmysqlclient.21.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-3
8-darwin.so
Reason: image not found"
I am able to post my entire (all be it super short) code file also if that will help resolve the problem. Thank you so much for your ideas and help!
I went thru the same issue, I found the best solutions within this question:
Python mysqldb: Library not loaded: libmysqlclient.18.dylib
I will recommend you to go thru all the options listed there, in my case run the command
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
solved my issues, most of the time when mysql gets installed the mysql directory should be renamed to just mysql.

how to install pygobject for python3 in fedora18

i try to install pygobject for python 3 in fedora 18
i read this doc https://python-gtk-3-tutorial.readthedocs.org/en/latest/install.html and i try to install jhbuild after that when i type $ jhbuild sanitycheck
get this error : bash: jhbuild: command not found...
i try to install with yum , i install "pygobject3-devel" but this not work with python3
in fedora 18 default python is python2.7.3, i install python3.3.0 with yum and test a first demo code :
#!/usr/bin/python
from gi.repository import Gtk
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
after give chmod +x for this python file can run with this command "./test.py"
and if i try to run with python (with out use #! ....) and type this command "$python test.py" that run and work without any problem but when i type this command "$python3 test.py" i get this error :
Traceback (most recent call last):
File "test.py", line 2, in <module>
from gi.repository import Gtk
ImportError: No module named 'gi'
have any idea ?
thanks
Try installing pygobject3 instead of pygobject3-dev (as a matter of fact, it seems that pygobject3 is already installed in a vainilla installation of Fedora).
After installing that package, you should be able to open your interpreter (python2.7) and do:
from gi.repository import Gtk
If you want to use python3 (I didn't understand if you meant that you tried to use python3 or you want to use python3), it seems that the gnome instrospection bindings are not yet available, according to this page that summarizes the state of different packages python3-related that are being on the works.
EDIT: I just tried installing python3-gobject (which should be installed when installing pygobject3, but it doesn't for some reason) and it seems to be working aswell on python3.

I am a newbie to python and I am having problems installing pygame?

I just downloaded pygame 32-bit on a 32-bit macintosh pro. I opened IDLE and typed in import pygame in IDLE shell then click enter. Usually I've heard that it should just return but it gives me the error message.
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pygame
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/__init__.py", line 95, in <module>
from pygame.base import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so, 2): no suitable image found. Did find:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so: no matching architecture in universal wrapper
Please help me!!!
I can't say if this is a problem of 32/64 bits installation, but you can easily check which version you are running :
import struct; print( 8 * struct.calcsize("P"))
I think it is because you typed py or python3 so it popped up >>> in your terminal so I would download it and do this:
Make sure there is no >>> if there is the only solution I found was the restart the terminal
First download it like this I will be using 3.9 for the version for example:
py -3.9 -m pip install pygame
then use this to check if it downloaded
py -3.9 -m pygame --version
Then open up IDLE - Python [VERSION] and then new file and type import pygame and this should work!

Installing MySQLdb Python Architecture Error

I am trying to install MySQLdb python and am having trouble getting it to work because I get an architecture error.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.6-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
File "build/bdist.macosx-10.6-x86_64/egg/_mysql.py", line 7, in <module>
File "build/bdist.macosx-10.6-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/jkeesh/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-x86_64.egg-tmp/_mysql.so, 2): no suitable image found. Did find:
/Users/jkeesh/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-x86_64.egg-tmp/_mysql.so: mach-o, but wrong architecture
This has been a common question, and I have read many, many sources including:
Python MySQL wrong architecture error
http://www.mechanicalgirl.com/view/installing-django-with-mysql-on-mac-os-x/
Django + MySQL on Mac OS 10.6.2 Snow Leopard
Installing MySQLdb on Mac OS X
and many other links.
It appears that the problem is a 32-bit 64-bit mismatch, but I'm not sure what the right combination is, or what exactly the mismatch is.
I have installed from pip, from Mac ports, and built from the source. I have tried setting the ARCHFLAGS to both i386 and and x86_64.
I have tried setting VERSIONER_PYTHON_PREFER_32_BIT and VERSIONER_PYTHON_PREFER_64_BIT preferences.
I am running Mac OS X 10.6.6
I have mysql installed in /usr/local/mysql
$ file $(which ./mysql)
./mysql: Mach-O executable i386
I have mysql version 5.5.12
I have 64 bit Python 2.6.6.
My system architecture is:
>>> platform.platform()
'Darwin-10.6.0-i386-64bit'
$ file $(which python)
/opt/local/bin/python: Mach-O 64-bit executable x86_64
If there are any links you can point me to or suggestions to try I would really appreciate it. I'm kind of at a dead end and getting the same "wrong architecture" error no matter what I try.
Try to add this in your .bashrc or .bash_profile:
PATH="/usr/local/mysql/bin:${PATH}"
export PATH
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
export VERSIONER_PYTHON_PREFER_64_BIT=no
export VERSIONER_PYTHON_PREFER_32_BIT=yes
I've solved my own question, so hopefully this can be of help to people with similar issues.
I believe I had a 32-bit mysql installation.
I uninstalled all versions of mysql that I had
I went back and installed mysql 5.1 from the source, but I think a mysql 5.1 x86_64 bit dmg or binary would work also.
I used this make command from this post http://hivelogic.com/articles/compiling-mysql-on-snow-leopard/
I had permission issues and finally got it working using
sudo mysqld_safe --skip-grant-tables &
I installed MySQL-python 1.2.3c1 from here
http://pypi.python.org/pypi/MySQL-python/1.2.3c1