Import Python module fails (http.cookies) - python

From what I've learned from my research here and elsewhere, it seems that if a) a module is located in the Python search path or b) contained in a package that is in the Python search path, that the import command should be able to find and import the module.
In the interactive script below, note that both of these conditions have been satisfied. the http folder contains an __init__.py file, making it a package, and that folder contains a module named cookies.py. Yet, the command import http.cookies fails, and the traceback looks like Python is searching for this module in the django folders, which I know will fail as there is no cookies.py module in the django http package folder. I also tried to manipulate the search path by editing my PYTHONPATH to ensure that the Standard Library http package folder is earlier in the search path, but as you can see below, the import is still failing.
What is causing this failure?
Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:25:12) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> for item in sys.path: print(item)
C:\Python33\Lib\idlelib
C:\Python33\lib\site-packages\setuptools-1.1.7-py3.3.egg
c:\Python33\Lib\http
C:\Python33\Lib\site-packages
C:\Python33\Lib\site-packages\django
C:\Python33\Lib\site-packages\django\bin
C:\Windows\system32\python33.zip
C:\Python33\DLLs
C:\Python33\lib
C:\Python33
C:\Python33\lib\site-packages\win32
C:\Python33\lib\site-packages\win32\lib
C:\Python33\lib\site-packages\Pythonwin
>>> import os
>>> os.path.isfile('C:/Python33/Lib/http/__init__.py')
True
>>> os.path.isfile('C:/Python33/Lib/http/cookies.py')
True
>>> import http.cookies
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
import http.cookies
File "C:\Python33\Lib\site-packages\django\http\__init__.py", line 1, in <module>
from django.http.cookie import SimpleCookie, parse_cookie
File "C:\Python33\Lib\site-packages\django\http\__init__.py", line 1, in <module>
from django.http.cookie import SimpleCookie, parse_cookie
File "C:\Python33\Lib\site-packages\django\http\cookie.py", line 5, in <module>
from django.utils.six.moves import http_cookies
File "C:\Python33\Lib\site-packages\django\utils\six.py", line 86, in __get__
result = self._resolve()
File "C:\Python33\Lib\site-packages\django\utils\six.py", line 105, in _resolve
return _import_module(self.mod)
File "C:\Python33\Lib\site-packages\django\utils\six.py", line 76, in _import_module
__import__(name)
ImportError: No module named 'http.cookies'
>>>

You added the Django top-level package to your sys.path:
C:\Python33\Lib\site-packages\django
Remove that entry, and don't add top-level packages to your path. Python finds the http top-level package in that directory first, so you are now effectively importing the django.http package, which does not have a cookies module.
You should only add the parent directory of a package to your path. C:\Python33\Lib\site-packages and C:\Python33\Lib are both already listed, so you do not need any of the following:
c:\Python33\Lib\http
C:\Python33\Lib\site-packages\django
C:\Python33\Lib\site-packages\django\bin
C:\Python33\lib\site-packages\win32
C:\Python33\lib\site-packages\win32\lib
C:\Python33\lib\site-packages\Pythonwin

Related

Cannot run cv2 in Anaconda [duplicate]

I have a script named requests.py that needs to use the third-party requests package. The script either can't import the package, or can't access its functionality.
Why isn't this working, and how do I fix it?
Trying a plain import and then using the functionality results in an AttributeError:
import requests
res = requests.get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
In more recent versions of Python, the error message instead reads AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import).
Using from-import of a specific name results in an ImportError:
from requests import get
res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get'
In more recent versions of Python, the error message instead reads ImportError: cannot import name 'get' from partially initialized module 'requests' (most likely due to a circular import) (/Users/me/dev/rough/requests.py).
Using from-import for a module inside the package results in a different ImportError:
from requests.auth import AuthBase
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package
Using a star-import and then using the functionality raises a NameError:
from requests import *
res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import *
File "/Users/me/dev/rough/requests.py", line 3, in <module>
res = get('http://www.google.ca')
NameError: name 'get' is not defined
This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.
An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:
Notice the name you used in your script:
File "/Users/me/dev/rough/requests.py", line 1, in <module>
The module you are trying to import: requests
Rename your module to something else to avoid the name collision.
Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.
In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.
The error occurs because a user-created script has a name-clash with a library filename. Note, however, that the problem can be caused indirectly. It might take a little detective work to figure out which file is causing the problem.
For example: suppose that you have a script mydecimal.py that includes import decimal, intending to use the standard library decimal library for accurate floating-point calculations with decimal numbers. That doesn't cause a problem, because there is no standard library mydecimal. However, it so happens that decimal imports numbers (another standard library module) for internal use, so a script called numbers.py in your project would cause the problem.
In one especially pernicious case, having a file named token.py in a project (or the current working directory, when starting up Python in interactive mode) causes the interactive help to break:
$ touch token.py
$ python
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help
Type help() for interactive help, or help(object) for help about object.
>>> help()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/_sitebuiltins.py", line 102, in __call__
import pydoc
File "/usr/lib/python3.8/pydoc.py", line 66, in <module>
import inspect
File "/usr/lib/python3.8/inspect.py", line 40, in <module>
import linecache
File "/usr/lib/python3.8/linecache.py", line 11, in <module>
import tokenize
File "/usr/lib/python3.8/tokenize.py", line 35, in <module>
from token import EXACT_TOKEN_TYPES
ImportError: cannot import name 'EXACT_TOKEN_TYPES' from 'token' (/current/working/directory/token.py)
The traceback tells us all we need to know: calling help triggers a deferred import of the standard library pydoc, which indirectly attempts to import the standard library token, but finds our token.py which doesn't contain the appropriate name. In older versions of Python, it was even worse: tokenize would do a star-import from token, and then its top-level code would try to use a name defined there, resulting in NameError - and a stack trace not mentioning the file name token.py.
If you still encounter problems like this after tracking own and renaming or removing the appropriate .py files in your project, also check for .pyc files that Python uses to cache bytecode compilation when importing modules. In 3.x, these will be stored in folders with the special name __pycache__; it is safe to delete such folders and files, and possible to suppress them (but you normally won't want to).

Flask not importing in virtualenv [duplicate]

I have a script named requests.py that needs to use the third-party requests package. The script either can't import the package, or can't access its functionality.
Why isn't this working, and how do I fix it?
Trying a plain import and then using the functionality results in an AttributeError:
import requests
res = requests.get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
In more recent versions of Python, the error message instead reads AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import).
Using from-import of a specific name results in an ImportError:
from requests import get
res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get'
In more recent versions of Python, the error message instead reads ImportError: cannot import name 'get' from partially initialized module 'requests' (most likely due to a circular import) (/Users/me/dev/rough/requests.py).
Using from-import for a module inside the package results in a different ImportError:
from requests.auth import AuthBase
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package
Using a star-import and then using the functionality raises a NameError:
from requests import *
res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import *
File "/Users/me/dev/rough/requests.py", line 3, in <module>
res = get('http://www.google.ca')
NameError: name 'get' is not defined
This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.
An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:
Notice the name you used in your script:
File "/Users/me/dev/rough/requests.py", line 1, in <module>
The module you are trying to import: requests
Rename your module to something else to avoid the name collision.
Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.
In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.
The error occurs because a user-created script has a name-clash with a library filename. Note, however, that the problem can be caused indirectly. It might take a little detective work to figure out which file is causing the problem.
For example: suppose that you have a script mydecimal.py that includes import decimal, intending to use the standard library decimal library for accurate floating-point calculations with decimal numbers. That doesn't cause a problem, because there is no standard library mydecimal. However, it so happens that decimal imports numbers (another standard library module) for internal use, so a script called numbers.py in your project would cause the problem.
In one especially pernicious case, having a file named token.py in a project (or the current working directory, when starting up Python in interactive mode) causes the interactive help to break:
$ touch token.py
$ python
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help
Type help() for interactive help, or help(object) for help about object.
>>> help()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/_sitebuiltins.py", line 102, in __call__
import pydoc
File "/usr/lib/python3.8/pydoc.py", line 66, in <module>
import inspect
File "/usr/lib/python3.8/inspect.py", line 40, in <module>
import linecache
File "/usr/lib/python3.8/linecache.py", line 11, in <module>
import tokenize
File "/usr/lib/python3.8/tokenize.py", line 35, in <module>
from token import EXACT_TOKEN_TYPES
ImportError: cannot import name 'EXACT_TOKEN_TYPES' from 'token' (/current/working/directory/token.py)
The traceback tells us all we need to know: calling help triggers a deferred import of the standard library pydoc, which indirectly attempts to import the standard library token, but finds our token.py which doesn't contain the appropriate name. In older versions of Python, it was even worse: tokenize would do a star-import from token, and then its top-level code would try to use a name defined there, resulting in NameError - and a stack trace not mentioning the file name token.py.
If you still encounter problems like this after tracking own and renaming or removing the appropriate .py files in your project, also check for .pyc files that Python uses to cache bytecode compilation when importing modules. In 3.x, these will be stored in folders with the special name __pycache__; it is safe to delete such folders and files, and possible to suppress them (but you normally won't want to).

Flask profling error: the profiler is not available because profile or pstat is not installed [duplicate]

I have a script named requests.py that needs to use the third-party requests package. The script either can't import the package, or can't access its functionality.
Why isn't this working, and how do I fix it?
Trying a plain import and then using the functionality results in an AttributeError:
import requests
res = requests.get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
In more recent versions of Python, the error message instead reads AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import).
Using from-import of a specific name results in an ImportError:
from requests import get
res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get'
In more recent versions of Python, the error message instead reads ImportError: cannot import name 'get' from partially initialized module 'requests' (most likely due to a circular import) (/Users/me/dev/rough/requests.py).
Using from-import for a module inside the package results in a different ImportError:
from requests.auth import AuthBase
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package
Using a star-import and then using the functionality raises a NameError:
from requests import *
res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import *
File "/Users/me/dev/rough/requests.py", line 3, in <module>
res = get('http://www.google.ca')
NameError: name 'get' is not defined
This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.
An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:
Notice the name you used in your script:
File "/Users/me/dev/rough/requests.py", line 1, in <module>
The module you are trying to import: requests
Rename your module to something else to avoid the name collision.
Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.
In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.
The error occurs because a user-created script has a name-clash with a library filename. Note, however, that the problem can be caused indirectly. It might take a little detective work to figure out which file is causing the problem.
For example: suppose that you have a script mydecimal.py that includes import decimal, intending to use the standard library decimal library for accurate floating-point calculations with decimal numbers. That doesn't cause a problem, because there is no standard library mydecimal. However, it so happens that decimal imports numbers (another standard library module) for internal use, so a script called numbers.py in your project would cause the problem.
In one especially pernicious case, having a file named token.py in a project (or the current working directory, when starting up Python in interactive mode) causes the interactive help to break:
$ touch token.py
$ python
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help
Type help() for interactive help, or help(object) for help about object.
>>> help()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/_sitebuiltins.py", line 102, in __call__
import pydoc
File "/usr/lib/python3.8/pydoc.py", line 66, in <module>
import inspect
File "/usr/lib/python3.8/inspect.py", line 40, in <module>
import linecache
File "/usr/lib/python3.8/linecache.py", line 11, in <module>
import tokenize
File "/usr/lib/python3.8/tokenize.py", line 35, in <module>
from token import EXACT_TOKEN_TYPES
ImportError: cannot import name 'EXACT_TOKEN_TYPES' from 'token' (/current/working/directory/token.py)
The traceback tells us all we need to know: calling help triggers a deferred import of the standard library pydoc, which indirectly attempts to import the standard library token, but finds our token.py which doesn't contain the appropriate name. In older versions of Python, it was even worse: tokenize would do a star-import from token, and then its top-level code would try to use a name defined there, resulting in NameError - and a stack trace not mentioning the file name token.py.
If you still encounter problems like this after tracking own and renaming or removing the appropriate .py files in your project, also check for .pyc files that Python uses to cache bytecode compilation when importing modules. In 3.x, these will be stored in folders with the special name __pycache__; it is safe to delete such folders and files, and possible to suppress them (but you normally won't want to).

Cannot import logging.py after I renamed one of my source files logging.py in my Eclipse project

I thought, I broke Eclipse by renaming one of my source file logging.py, I quickly changed the name to something else but Eclipse cannot find the original python standard file ... I reinstalled my Anaconda install but it did not correct the problem ... and I later discovered it was likely a Python problem (see Edit 1)
When I ask Eclipse to find or open the file from the 'import logging' line, it seems it cannot find it ...
Any idea to correct this problem without reinstalling Eclipse?
Traceback (most recent call last):
File "C:\Users\ailete\workspace\landema\main.py", line 56, in <module>
import requests
File "C:\Users\ailete\Anaconda2\Lib\site-packages\requests\__init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "C:\Users\ailete\Anaconda2\Lib\site-packages\requests\packages\__init__.py", line 27, in <module>
from . import urllib3
File "C:\Users\ailete\Anaconda2\Lib\site-packages\requests\packages\urllib3\__init__.py", line 8, in <module>
from .connectionpool import (
File "C:\Users\ailete\Anaconda2\Lib\site-packages\requests\packages\urllib3\connectionpool.py", line 35, in <module>
from .connection import (
File "C:\Users\ailete\Anaconda2\Lib\site-packages\requests\packages\urllib3\connection.py", line 43, in <module>
from .util.ssl_ import (
File "C:\Users\ailete\Anaconda2\Lib\site-packages\requests\packages\urllib3\util\__init__.py", line 19, in <module>
from .retry import Retry
File "C:\Users\ailete\Anaconda2\Lib\site-packages\requests\packages\urllib3\util\retry.py", line 15, in <module>
log = logging.getLogger(__name__)
AttributeError: 'module' object has no attribute 'getLogger'
Edit 1
The problem happens with the executable generated by pyinstaller too ?! :
...
LOADER: Running main.py
Traceback (most recent call last):
File "<string>", line 43, in <module>
File "c:\users\ailete\appdata\local\temp\pip-build-iulqaq\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 363, in load_module
File "C:\Users\ailete\workspace\landema\entities.py", line 7, in <module>
from config import locale, ribbon_menu
File "c:\users\ailete\appdata\local\temp\pip-build-iulqaq\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 363, in load_module
File "C:\Users\ailete\workspace\landema\config.py", line 7, in <module>
import logging
ImportError: No module named logging
main returned -1
...
First, define modules path, where built-in module logging is placed:
user#host:$ python
Python 2.7.9 (default, Mar 1 2015, 12:57:24)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['/usr/lib/python2.7', ... ]
Second, ensure that there is a logging package. In my OS i have /usr/lib/python2.7/logging. I think you renamed the package accidentally; If so, rename it back.
I came across a very similar problem. The error I was receiving was: "AttributeError: ‘flask.logging’ object has no attribute ‘getLogger’". And the solution was downgrade the package Flask (settings -> project -> project interpreter).
Sorry, I solved it myself.
I thought Eclipse delete the .pyc files when I told it to clean the project, but it is not how it works apparently, So after 2 reinstalls of anaconda and configuration tweaks for this project (I have others ...), I finally remembered to check that ... the .pyc for the file was still there with the old name 'logging.pyc' ... thanks Eclipse as always, for wasting my time with your quirks!

inconsistent behaviour of interactive session and script in Python

When I run the script, I had this ImportError:
$ python ~/Dropbox/code/py/ZoteroFindOrphanedFiles.py
Traceback (most recent call last):
File "/home/zane/Dropbox/code/py/ZoteroFindOrphanedFiles.py", line 1, in <module>
import sqlite3
File "/usr/lib/python3.2/sqlite3/__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "/usr/lib/python3.2/sqlite3/dbapi2.py", line 23, in <module>
import datetime
File "/usr/lib/python3.2/datetime.py", line 20, in <module>
import math as _math
File "/home/zane/Dropbox/code/py/math.py", line 3, in <module>
from nzmath.rational import Integer, Rational
ImportError: No module named nzmath.rational
But I don't have it when running the interactive session:
$ python
Python 3.2.3 (default, Apr 23 2012, 23:14:44)
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>>
Why is that?
Here is the problem:
import math as _math
File "/home/zane/Dropbox/code/py/math.py", line 3, in <module>
You have your own module called math.py, but this is the same as a standard Python module of the same name. This is not recommended.
The solution is to rename your math.py to something else, and don't forget to delete the math.pyc in the same directory (otherwise you'll still have the same problem).
You have a local file
/home/zane/Dropbox/code/py/math.py
which is getting imported instead of the standard lib math module.
The solution is to rename your /home/zane/Dropbox/code/py/math.py to something else.
The problem occurs when you call a script in the /home/zane/Dropbox/code/py directory since this adds this directory to the beginning of sys.path and thus this directory gets searched first when Python tries to import modules.
You have a file called math.py in the script's directory, and this shadows the stdlib math module. Rename the file.

Categories