Python 3 mySQL.connector frozen import lib - python

I am new to Python 3 and still learning but I need help. The first part of the script is:
import mysql.connector #this is failing as a .py but works in the shell
cnx = mysql.connector.connect(user='root', password='mypassword', host='my_ip_address', database'name_of_database') #this works in the shell
cursor = cnx.cursor()
I have tried the above line by line in the Python shell which works fine: I can import the connector to connect to my database and fetch back data. But when I save the script as a .py the import mysql connector does not work. I do have the Path variable setup and when I installed the mySQL connector it placed the relevant files in my Python install path \Lib\site-pacakges folder.
I get the following error:
>Traceback (most recent call last):
> File "<frozen importlib._bootstrap>", line 1518, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
>Traceback (most recent call last):
> File "C:\Users\Paul\Desktop\scripts\mysql.py", line 2, in <module>
> import mysql.connector
> File "C:\Users\Paul\Desktop\scripts\mysql.py", line 2, in <module>
> import mysql.connector
>ImportError: No module named 'mysql.connector'; mysql is not a package

I have the same question as your, and I found an answer in another website where you asked the same question. I'm pasting the answer here as reference.
The above happens because your script is named 'mysql.py', which is
actually the same name as the 'mysql' package. Python correctly
reports ImportError. Simply rename your 'mysql.py' to something else,
like 'mysqlhacks.py'.
Taken from https://answers.launchpad.net/myconnpy/+question/226992

Related

ImportError: DLL load failed: The specified module could not be found in PyCharm

I am trying to work with SQLite from PyCharm. I downloaded SQLite, extract it and put it in C:/. Then I changed the 'PATH' to C:/. Then I opened PyCharm and write this code:
import sqlite3
connection = sqlite3.connect('db/test.db')
cursor = connection.cursor()
And I have got this error:
Traceback (most recent call last):
File "C:/Users/nadav/.PyCharmCE2019.1/config/scratches/scratch_1.py", line 1, in <module>
import sqlite3
File "C:\Users\nadav\Anaconda3\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "C:\Users\nadav\Anaconda3\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed: The specified module could not be found.
What am I missing?
I came across the same issue when I was running a code in the windows CMD prompt. The work-around i found was to run the same script in a 'Anaconda prompt'
Hope this helps...

Import http.client encouter import error with Python 3.4.1

I am following the example from the python online document(21.12.3) for practice. When I try to run my script with Run Module(F5), I always get an import error. But if I type them directly into IDLE command line, python does not complain. I am not sure what am I doing wrong.
The python version I am using is
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
The script is
import http.client
conn = http.client.HTTPConnection("192.168.1.2", 8080)
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print(r1.status, r1.reason)
conn.close()
The error message is
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\User\Downloads\http.py", line 1, in <module>
import http.client
File "D:\User\Downloads\http.py", line 1, in <module>
import http.client
ImportError: No module named 'http.client'; 'http' is not a package
You probably have created a Python script and named it http.py in local directory. This conflicts with Python 3's builtin module with the same name and leads to this error. Quick solution is to rename your file to something less generic to avoid conflict with Python builtin modules.
But if you insist, you can clear the name ambiguity by fully qualifying the local python module name using absolute imports:
from . import http
or less confusingly:
from . import http as myhttp
or
from .http import something
On Python 2, it is necessary to enable the absolute import feature at the very top of the importing module using a future statement before using this feature:
from __future__ import absolute_import
I had the same problem. In my case, there was another file named http.py in the same folder. I just renamed it, problem solved.

Can't get the "stem" module to work

I installed the stem module and did some copy/paste from the tutorials on their official site. None of them worked here.
In fact it doesn't even work when I type "from stem.control import Controller" in the command line. That gets me the following "error code":
>>> from stem.control import Controller
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1521, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from stem.control import Controller
File "C:\Python33\_PROJECTS\stem.py", line 6, in <module>
import build.lib.stem.process
File "C:\Python33\lib\build\lib\stem\__init__.py", line 421, in <module>
import stem.util.enum
ImportError: No module named 'stem.util'; stem is not a package
I'm using Python 3 and stem is supposed to work with it. Am I missing out something super obvious here?
From your traceback, what is this file?
File "C:\Python33\_PROJECTS\stem.py"
You are probably attempting to import this file, instead of the actual package (The last line of the traceback reveals so much).
Be careful when naming scripts identical to package name: the current working directory is added to the front of sys.path, and thus, such a script may get imported instead of the actual package. I assume you actually tried the imports from the C:\Python33\_PROJECTS\ directory.

Python 3.3 - urllib.request - import error

When I try to run the following Python 3.3 code on OS X 10.8 in PyCharm 2.7 (or run the .py file with the Python 3.3/2.7.3 launcher):
import urllib.request
f = urllib.request.urlopen('http://www.python.org/')
print(f.read(300))
I get the following error message:
/System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3 /Users/username/PycharmProjects/urllib/urllib.py
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1512, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/username/PycharmProjects/urllib/urllib.py", line 3, in <module>
import urllib.request
File "/Users/username/PycharmProjects/urllib/urllib.py", line 3, in <module>
import urllib.request
ImportError: No module named 'urllib.request'; urllib is not a package
Process finished with exit code 1
The only way I can succesfully run the code is via the Python shell.
Any ideas on how to solve this?
Thanks.
I changed the filename to url.py, now it succesfully executes in PyCharm.
But when executing the file via Python Launcher 3.3 it gives me the following error:
File "/Users/username/PycharmProjects/urllib/url.py", line 3, in <module>
import urllib.request
ImportError: No module named request
Why is the code running fine in PyCharm (3.3) but giving me an error when launched with Python Launcher (3.3)?
You named your file urllib, it is shadowing the standard library package. Rename your file.
In mac, VSCode is using Python 2. If you print urllib module, you will find out that this module is for Python 2.
So, I added one comment to choose Python version:
#!/usr/local/bin/python3
from urllib.request import urlopen
In my case, it waw solved activating in command palette (shift+commant+p) Linting: Enable Linting: on and in VMStudio Settings (Preferences => Settings) select flake8 in Python:linter => Linter to use as flake8.

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