Python AttributeError: 'module' object has no attribute 'connect' - python

I'm trying to create a program with sqlite3 database using Ubuntu (Xubuntu 14.04) and the pre-installed version of Python. I tried if the first lines are working but there is already an error. I installed "python-sqlite" and "sqlite3". Can anyone help?
import sqlite3
connection = sqlite3.connect('test.db')
cursor = connection.cursor()
cursor.execute('CREATE TABLE test ( id INTEGER, first INTEGER, second TEXT, third TEXT, other INTEGER)')
connection.commit()
The output is:
user#device:~/folder$ python sqlite3.py
Traceback (most recent call last):
File "sqlite3.py", line 1, in <module>
import sqlite3
File "/home/michael/ownCloud/sqlite3.py", line 3, in <module>
connection = sqlite3.connect('test.db')
AttributeError: 'module' object has no attribute 'connect'
Thank's in advance!

The error message shows you've named a file sqlite3.py:
/home/michael/ownCloud/sqlite3.py"
which masks the standard module of the same name. Your sqlite3.py does not define connect, hence the error.
The solution is to rename your file to something else.
As Jim Raynor points out, importing sqlite3 will also create a .pyc file in /home/michael/ownCloud/ which would also have to be deleted before the sqlite3 module in the standard lib can be found.

You need to change your script name. sqlite3 is the name of your script and of the package you want to import, so Python import your script instead of the package, hence the error.

Rename your script from 'sqlite3.py' to 'something else.py'.Python interpreter is having a hard time distinguishing your script and the inbuilt module sqlite3.
The line 'AttributeError: 'module' object has no attribute 'connect' tells you that when you run your script it reads itself assuming it is the inbuilt sqlite3 module which obviously doesn't contain the attribute connect.

I cannot answer to your input in here, but I got it sorted now.
It is as you say, but renaming the original file is not enough.
You have to delete the *.pyc file too. I only renamed the the offendingt
filename and since it did not work I searched for other options.
This was tricky because the errormessage was missleading.
Anyway. I have sort it out now.
Thanks for your input.
This is it, normaly when you code you often type out the filename
to what you are about to code, for example: Hello World.
So the filename is named helloworld.py
And in this spirit of nameconvention I named my Pythonfile
sqlite3.py
I had no idea that decision would give me so much grief
I wrote the appropriate code and couldn't for some reason get passed the error
No Module found Error: sqlite3
This clearly tells me that, for some reason I don't have that module.
Anyway, using pip command freeze, is going to sort this out.
Strangely enough, no module in the list with sqlite3. That send me on a wildgoose-chase.
Apparantly I am missig this module.
That's strange since I have a Python ver. 3.8.2
and it is equipped with an build-in sqlite3 module.
But I could not verify it.
I searched everywhere, and all suggestions I found was leaning towards that
something was missing in my Pythoninstallation. I'll tried everything. To no avail.
Then after some 2-3 days of research. I found this thread.
It said that Python cannot distinguish between a modulename or a filename,
if they are having exactly the same names.
By following the solution here. So easy.
Save your sqlite3.py file as a new filename, something different.
then find your Python cache-file named *.pyc, delete the *.pyc file.
Now everything works.
So even if the module sqlite3 did not get listed via pip freeze command it
was apparently there the whole time build-in to another module.
Note that I did not reinstall anything or added other modules to get this to work.
Just renamed the py-filename or saved under a different name and
deleted the *.pyc file. No other alternations.

I was facing same error but finally i got solution that :
"Copy and paste your program in another .py file then delete previous file where you had faced error save that and run"
And problem will be solved

Related

Python module not getting deleted properly

Somehow I ended up installing 2 different modules named 'google'. One is present in site_packages folder inside python, and the one in my custom path provided in PYTHONPATH.
I wanted to get rid of the one in site_packages. So I deleted that folder (as per other SO answers). But now, I am facing this weird scenario that it still loads the wrong on at start
>>> import google; google.__path__
['/Library/Python/2.7/site-packages/google']
>>> google.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
>>> reload(google)
<module 'google' from '/custom_path/google-cloud-sdk/platform/google_appengine/google/__init__.pyc'>
>>> google.__path__
['/custom_path/google-cloud-sdk/platform/google_appengine/google'
>>> google.__file__
'/custom_path/google-cloud-sdk/platform/google_appengine/google/__init__.pyc'
So basically, first time it's still loading the old module (with no files in it), and on reloading, it's giving the correct one.
My question is how to delete the old one completely as it's not feasible to always reload module like this.
The problem was that somehow other modules were holding a reference to the google module. I removed all packages from google and re-installed them and it worked perfectly after that. I am not yet sure why & how the module was still being referenced, so would appreciate if anyone can throw light to that as well.

Problems importing a python module in Maya and it being recognized

Still learning certain things about Python... I am having issues recognizing my Python script in my scripts dir. First, I checked to see that my path is set correctly:
import sys
for pythonPath in sys.path:
print pythonPath
And C:/Users/..../Documents/maya/2014-x64/scripts is listed, which is where I am placing swap.py
In Maya's script editor I am typing the following:
import swap
reload(swap)
swap.printSomething()
I get:
Error: AttributeError: file line 3: 'module' object has no attribute 'printSomething' #
If I take the same code and throw it into a package...
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py
And then call this, it works...
import swapPackage.swap as swap
reload(swap)
swap.printSomething()
Why? I am totally confused. Mel scripts even run fine from this location as well. I just can't get a simple python script to import and run.
Also something I noticed. Even though I can get this script to run in a package, the package name must be totally different than the module name. I can't have a package named this:
C:/Users/..../Documents/maya/2014-x64/scripts/swap/swap.py
but I can have one where the package name is different:
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py
Ok folks, I was able to solve this by executing a print of my file, only to find out that it was sourcing a totally different version someone copied elsewhere. ARGH. This solves both issues, and makes sense why changing the package name from the module worked.
import swap
reload(swap)
print swap.__file__

Override installed module in import

I am currently working on a package that's nearly ready for distribution. As such, I'm trying out installing it (via setup.py) on my system. However, I also need access to the current (source code) package, e.g. for tests.
Is it possible to explicitly force importing the local package instead of the installed version? What I already tried is adding the path at the beginning of sys.paths, with no luck.
I guess this question goes into how exactly Python looks for the modules to import, and how to change the order.
EDIT : It was a silly mistake, adding at the beginning of sys.paths works. I had some other import statement elsewhere that was executed first.
This is bad bad idea to do something like force importing the local package.
Python3 always import the built-in modules first, and obviously you're using Python3 cause Python2 is local-first.
I don't know how to solve this problem, if I were you I'd rename that file.
edit
You don't use append cause append is to the end. Use insert.
Tested on my cpu. I created a requests.py in another folder.
>>> import sys
>>> sys.path.insert(0 ,"path-to-another-folder/")
>>> import requests
>>> requests.get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'get'
>>>
If I don't do sys.path.insert(0 ,"path-to-another-folder/"), requests is imported normally.

How to fix error "AttributeError: 'module' object has no attribute 'client' in python3?

The following is my code.
import http
h1 = http.client.HTTPConnection('www.bing.com')
I think it's ok.But python give me the following error:
AttributeError: 'module' object has no attribute 'client'.
I wanted to know why and how to fix it.Thanks.
First, importing a package doesn't automatically import all of its submodules.*
So try this:
import http.client
If that doesn't work, then most likely you've got a file named http.py, or a directory named http, somewhere else on your sys.path (most likely the current directory). You can check that pretty easily:
import http
http.__file__
That should give some directory like /usr/lib/python3.3/http/__init__.py or /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/__init__.py or something else that looks obviously system-y and stdlib-y; if you instead get /home/me/src/myproject/http.py, this is your problem. Fix it by renaming your module so it doesn't have the same name as a stdlib module you want to use.
If that's not the problem, then you may have a broken Python installation, or two Python installations that are confusing each other. The most common cause of this is that installing your second Python edited your PYTHONPATH environment variable, but your first Python is still the one that gets run when you just type python.
* But sometimes it does. It depends on the module. And sometimes you can't tell whether something is a package with non-module members (like http), or a module with submodules (os). Fortunately, it doesn't matter; it's always save to import os.path or import http.client, whether it's necessary or not.

Why doesn't import work for me? - Python

Whenever I try to import a file into python, it comes up with this error(or similar):
Traceback (most recent call last):
File "C:/Python33/My Files/username save.py", line 1, in <module>
import keyring.py
ImportError: No module named 'keyring'
I am trying to create a password storing program, and I was looking up for good ways to keep passwords secure, and someone said use import keyring, so I did, except, it never works. I must be doing something wrong, but whenever I look anything up for python, it never works out for me. It's almost as if loads have things have been changed over the years.
and idea's?
The keyring module is not part of the Python standard library. You need to install it first. Installation instructions are included.
Once installed, use import keyring, not import keyring.py; the latter means import the py module from the keyring package. Python imports should use just the name of the module, so not the filename with extension. Python can import code from more than just .py python files.
I was getting the same error "ModuleNotFoundError: No module named 'keyring'". And after installing this module pip install keyring, the same error occured with another module name. Then I came to the conclusion that it is the fact that the VSCode is not able to connect to my venv, even after setting the Python Itereptor. Press CTRL + SHIFT + P, and then type Python: Select Interceptor, and select your venv, if you want to set this.
To fix the issue, I had to force the VSCode to use the .venv I created, and luckily there is a dropdown to do that, on the top right corner as in the preceeding image. Click ont the Python version, and then you will be able to select your virtual environment.
Now it will take the modules from your virtual environment.

Categories