SQLAlchemy import error : import _mysql - python

I am new at Python and SQLAlchemy and I was trying to play with them a little bit, but whenever I run a test it gives me the following error :
Traceback (most recent call last):
File "/home/zakaria/workspace-python/Jerreb/essai/tejriba.py", line 11, in <module>
engine = create_engine("mysql://root:root#localhost/python")
File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.9.7-py2.7-linux-i686.egg/sqlalchemy/engine/__init__.py", line 346, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.9.7-py2.7-linux-i686.egg/sqlalchemy/engine/strategies.py", line 74, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.9.7-py2.7-linux-i686.egg/sqlalchemy/connectors/mysqldb.py", line 64, in dbapi
return __import__('MySQLdb')
File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.5-py2.7-linux-i686.egg/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: /usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.5-py2.7-linux-i686.egg/_mysql.so: undefined symbol: _Py_ZeroStruct
I am working with PyDev.
This is the project architecture:
And these are the libs I have imported :
What is the problem and how can I fix it?
Thanks!

When I got this message, it turned out that, unbeknownst to me, the code was being run in the Python 3 interpreter, instead of Python 2. (This happened because I had installed Ubuntu's libapache2-mod-wsgi-py3 for a different project.) MySQL-python does not yet support Python 3. :'(
I don't know whether WSGI is involved for you, but try making sure you're not running Python 3.

Related

Python version error when running the Splunk TA-geoip2 app

I am using Splunk 8+ and Python 3.6. I am trying to run this https://github.com/0x616c6578/TA-geoip2 plugin.
I get this error message when I look into search.log and try to run the Python command in the log by itself:
C:\Users\strozllc>"C:\Program Files\Splunk\bin\Python3.exe" "C:\Program Files\Splunk\etc\apps\TA-geoip2-main\bin\geoip-command.py
Traceback (most recent call last):
File "C:\Program Files\Splunk\etc\apps\TA-geoip2-main\bin\geoip-command.py", line 8, in <module>
from splunklib.searchcommands import \
File "C:\Program Files\Splunk\etc\apps\TA-geoip2-main\bin\..\lib\splunklib\searchcommands\__init__.py", line 145, in <module>
from .environment import *
File "C:\Program Files\Splunk\etc\apps\TA-geoip2-main\bin\..\lib\splunklib\searchcommands\environment.py", line 20, in <module>
from logging.config import fileConfig
File "C:\Program Files\Splunk\Python-3.7\lib\logging\config.py", line 30, in <module>
import logging.handlers
File "C:\Program Files\Splunk\Python-3.7\lib\logging\handlers.py", line 26, in <module>
import logging, socket, os, pickle, struct, time, re
File "C:\Program Files\Splunk\Python-3.7\lib\socket.py", line 49, in <module>
import _socket
ImportError: Module use of python27.dll conflicts with this version of Python
n.
Huh? I looked at line 8 in geocommand.py and it is from
splunklib.searchcommands import \
dispatch, StreamingCommand, Configuration, Option, validators
I look it C:\Program Files\Splunk\bin, and there is indeed a python27.dll. But how is it getting called?
This is a case of Python 3 trying to run Python 2 code. The latest versions of Splunk (you didn't say which one you're using) only support Python 3 so an error will be thrown when an outdated library file is encountered.
I'd suggest filing an issue on GitHub, but the app appears to be abandonware since it hasn't been touched in a year despite having 4 issues.
Consider forking the code and updating it yourself.

How to troubleshoot import errors? Importing flask throws exception - ubuntu 18.04 [duplicate]

This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 4 years ago.
I am new to python programming and started trying to fool around with flask this morning. I installed flask using pip and it seemed to work as expected. However, when I went to the python shell and typed import flask I got the following error:
>>> import flask
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/flask/__init__.py", line 17,
in <module>
from werkzeug.exceptions import abort
File "/usr/lib/python2.7/dist-packages/werkzeug/__init__.py", line
151, in <module>
__import__('werkzeug.exceptions')
File "/usr/lib/python2.7/dist-packages/werkzeug/exceptions.py", line
71, in <module>
from werkzeug.wrappers import Response
File "/usr/lib/python2.7/dist-packages/werkzeug/wrappers.py", line
27, in <module>
from werkzeug.http import HTTP_STATUS_CODES, \
File "/usr/lib/python2.7/dist-packages/werkzeug/http.py", line 23,
in <module>
from email.utils import parsedate_tz
File "/usr/lib/python2.7/email/utils.py", line 27, in <module>
import random
File "random.py", line 6, in <module>
print(random.randint(1, 101))
AttributeError: 'module' object has no attribute 'randint''
I uninstalled flask using pip and tried again. No change. I installed flask using apt, same problem I even tried it on python 2, same error message. the really weird part is that it is giving me errors from a python3 shell session I did earlier in the week - the import random for random.py part of the message at the very end. Why would it spit out messages that have nothing to do with the import flask message?
It made me think that maybe I should see if a reboot would help, but no luck there either.
How does one troubleshoot an issue like this? googling the error hasn't helped and I am running out of ideas.
Thanks in advance
From what it looks like the error message you are receiving means you have a file name random.py being invoked. This can happen if the file is in the same folder you are running the command python2 or python3.
According to the error message:
print(random.randint(1, 101))
AttributeError: 'module' object has> no attribute 'randint''
It's trying to load that file. Rename your file to something else and it should work.
If you locate random.py in linux you will see it here (depending on your versions):
/usr/lib/python2.7/random.py
and
/usr/lib/python3.6/random.py
So, your random.py is causing the python command to override the base python random.py file

Python shell working differently on different location in the same machine inside same virtual environment

I have a cloud instance of a Linux machine (openSuSE) with multiple users.
I have created a virtual environment and installed all my required libraries (including Klein).
I have two users "a" and "b".
While logged in as "a" and inside virtualenv, when I open python shell at home directory and type
import klein
it imports normally.
Now when I change directory to
/home/b/
and run the same (open python shell, import klein) while being in the same virtualenv, it gives me an error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/a/.local/lib/python3.6/site-packages/klein/__init__.py", line 3, in <module>
from klein._plating import Plating
File "/home/a/.local/lib/python3.6/site-packages/klein/_plating.py", line 16, in <module>
from .app import _call
File "/home/a/.local/lib/python3.6/site-packages/klein/app.py", line 19, in <module>
from twisted.internet import endpoints, reactor
File "/home/a/.local/lib/python3.6/site-packages/twisted/internet/endpoints.py", line 58, in <module>
from twisted.protocols.tls import TLSMemoryBIOFactory
File "/home/a/.local/lib/python3.6/site-packages/twisted/protocols/tls.py", line 63, in <module>
from twisted.internet._sslverify import _setAcceptableProtocols
File "/home/a/.local/lib/python3.6/site-packages/twisted/internet/_sslverify.py", line 158, in <module>
verifyHostname, VerificationError = _selectVerifyImplementation()
File "/home/a/.local/lib/python3.6/site-packages/twisted/internet/_sslverify.py", line 141, in _selectVerifyImplementation
from service_identity import VerificationError
File "/home/a/.conda/envs/mm/lib/python3.6/site-packages/service_identity/__init__.py", line 7, in <module>
from . import cryptography, pyopenssl
File "/home/a/.conda/envs/mm/lib/python3.6/site-packages/service_identity/cryptography.py", line 16, in <module>
from .exceptions import SubjectAltNameWarning
File "/home/a/.conda/envs/mm/lib/python3.6/site-packages/service_identity/exceptions.py", line 21, in <module>
#attr.s
AttributeError: module 'attr' has no attribute 's'
Command "which python" gives same address at both location which is my virtualenv python address and that should be expected.
But what causes this weird python shell behavior.
Thank you
I solved it and a very shameful reason caused the error.
One of the modules Twisted uses is "attr" module. I had named one of my files attr.py and that is what was causing all the error.
I myself am not deleting this question if moderation has no problem, maybe somebody like me might be stuck at the same situation. It may help them.
Never name your python files same as that of any standard module unless overriding.
Also if your issue persists, then Jean's answer will definitely resolve it.
There can be multiple different Python packages that provide the same Python module. For example, there are at least two packages that provide the attr module:
https://pypi.org/project/attr/
https://pypi.org/project/attrs/
It's possible you've installed the wrong package based on the requirements. You can check what you have installed with pip freeze.

Python error: No module named RuntimeError

Im trying to figured out this problem. Yesterday I installed PyScripter and since then, scripts doesnt work. When I run any script (in PyScripter or IDLE) and trying to "import arcpy", it gets this error:
import arcpy
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import arcpy
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 17, in <module>
from geoprocessing import gp
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\__init__.py", line 14, in <module>
from _base import *
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 14, in <module>
import arcgisscripting
File "C:\Python26\ArcGIS10.0\lib\arcgisscripting.py", line 81, in <module>
from RuntimeError import RuntimeError
ImportError: No module named RuntimeError
Have somebody any suggestion how to fix it?
Thanks to all very much!
Sounds like the module is not installed properly (or at all). To verify, do the following:
open a shell and start the python shell by typing python
(If this doesn't display an error, check that python is added to your system path or if it is even installed at all. But if you've used Python on said machine maybe system path issue)
enter the commmand help('modules')
review the list of modules that's returned to see whether arcpy is included
if not, then you may have to reinstall the module

SQLAlchemy import errors in Pydev/Eclipse

I tried installing SQLAlchemy for Python 3.2 which I use with Eclipse/Pydev. A simple test script fails
from sqlalchemy.engine import create_engine
engine=create_engine("mysql://user:password#server/database")
If I run it from Eclipse I get
Traceback (most recent call last):
File "...\sqlalchemy.py", line 1, in <module>
from sqlalchemy.engine import create_engine
File "...\sqlalchemy.py", line 1, in <module>
from sqlalchemy.engine import create_engine
ImportError: No module named engine
However I actually generated the import line with Ctrl-Shirt-O, so Eclipse found that automatically and knows about it. Also Pydev does not show any errors in the script.
If I try the same script in the interactive Pydev console I get
from sqlalchemy.engine import create_engine
engine=create_engine("mysql://user:password#server/database")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python32\lib\site-packages\sqlalchemy-0.7.8-py3.2.egg\sqlalchemy\engine \__init__.py", line 338, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Python32\lib\site-packages\sqlalchemy-0.7.8-py3.2.egg\sqlalchemy\engine\strategies.py", line 64, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "C:\Python32\lib\site-packages\sqlalchemy-0.7.8-py3.2.egg\sqlalchemy\connectors\mysqldb.py", line 52, in dbapi
return __import__('MySQLdb')
ImportError: No module named MySQLdb
Do you have an idea how to get it work?
Answer is simple: your main module is named sqlalchemy.py. This is a trap that was much more easier to fall in python 2 - naming your own module by the same name as a system module.
At startup your sqlalchemy.py is loaded by python as the __main__ module, and when the first line runs, python reloads sqlalchemy.py as the module sqlalchemy; the second time the import line is run the python interpreter already finds sqlalchemy in sys.modules, but it does not contain the variable or module named engine.
For easy fix rename sqlalchemy.py to for example satest.py. For more complete solution, organize your code in packages.
While the first error was an unfortunate mistake as explained by Antti I finally also solved the other issue. I didn't have MySQLdb installed which again requires a MySQL Server. Instead I have mysql-connector for which the correct syntax is
engine=create_engine("mysql+mysqlconnector://...")
I didn't see this while looking for quick test examples.

Categories