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.
Related
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.
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
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.
When i compile s3cmd, I found Versioning module is missing, stacktrace as:
Traceback (most recent call last):
File "./s3cmd", line 1983, in <module>
from S3.S3 import S3
File "/home/chutong/s3cmd.svn/S3/S3.py", line 29, in <module>
from Versioning import Versioning
ImportError: No module named Versioning
I tried to check online and there is not much resource? Can someone please help? Thanks
I just checked the source for s3cmd on github. It appears it no longer imports this module nor does it contain a module named Versioning in its code tree.
Without more information I can't tell, but what I would suspect is you have a version mismatch, perhaps one version installed in your Python environment and another one locally.
It's a bit odd that it's raising the exception from an SVN directory, but like I say, will need more information about your execution environment to make any headway. Things like current directory, a dump of sys.path, that sort of thing.
But if it's possible, you might try updating the installed version and trying again. Looks like the library has had some significant updates since the version you appear to be using.
While reading the code for django/forms/widgets.py, I saw:
from util import flatatt
To dig deeper, I tried to import the util module in a Python shell in my terminal, but I got an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named util
This left me confused. What is wrong?
Django is (ab?)using Python's import system in order to import something from django.forms.util. Import that module instead.
In python, you get this ImportError:
Traceback (most recent call last):
File "b.py", line 3, in <module>
from util import flatatt
ImportError: No module named util
Some programmer out there created a library for your python script to use. Your program was unable to find that library. So the interpreter tells you that it can't be found. You have a few options. Either you have to define the library and functionality yourself, or you'll have to review the instructions and source code of the program you are trying to run, and try to figure out what is broken to prevent the library from being included. Or the final option is to remove any usage of that library in the current script.
You can define the module yourself like this:
Put this in util.py in the same directory as your widgets.py file.
def flatatt(prompt):
print("ok")
If you define that, you will get a different error, the script will import your library, and find that method, but then will do the wrong thing, because the programmer who designed that code probably had some other functionality in mind.
You need to figure out what you did wrong, or what the original programmer did wrong, or how your specific system is different to cause this to work.
You can learn more about what I've described above here, by rolling your own python modules: Python: How to import other Python files
Often times these sorts of bugs are barriers to entry, the original developers don't want programmers who don't know the difference between an integer and a module using the software. So little problems like this are added, to help encourage you that you need to develop a better understanding of the source code under the hood. It's like a mechanic helping everyone become better mechanics by swapping the wires on the distributor cap. It isn't going to work until you swap them back rightly.
from django.forms.utils import flatatt
It worked with django 1.11 version, and may work with 1.8 through 1.10