Python import statement throws exception - python

I have some code that looks like this
try:
import calvin.reasoning.rule_list
except:
print "rule_list exception"
And I have commented out every import statement in rule_list.py, so it looks like this
#import quantities
#import rules
#from conclusions import Conclusion
#from conclusions import Data
#from confidence import Validity
If I delete the contents of rule_list my program runs, but why would python throw an exception if there is not a circular import?
Here is the traceback
Traceback (most recent call last):
File "/home/paul/projects/branchCalvin/Calvin/src/cscience/components/c_calibration.py", line 18, in <module>
import calvin.reasoning.rule_list
File "/home/paul/projects/branchCalvin/Calvin/src/calvin/reasoning/rule_list.py", line 37, in <module>
import rules
File "/home/paul/projects/branchCalvin/Calvin/src/calvin/reasoning/rules.py", line 32, in <module>
import engine
File "/home/paul/projects/branchCalvin/Calvin/src/calvin/reasoning/engine.py", line 33, in <module>
import conclusions
File "/home/paul/projects/branchCalvin/Calvin/src/calvin/reasoning/conclusions.py", line 31, in <module>
import samples
File "/home/paul/projects/branchCalvin/Calvin/src/calvin/reasoning/samples.py", line 30, in <module>
from cscience import datastore
ImportError: cannot import name datastore
problem importing module components.c_calibration
(<type 'exceptions.ImportError'>, ImportError('cannot import name datastore',), <traceback object at 0x9ebc554>)

Related

cannot import numpy when script located in a subdirectory

I have a folder structure like so
root/
A0/
A1/
B0/
B1/
Lets say I have a file called test.py. In it, I import numpy like so
import numpy as np
That is all that is contained in the file. This works without issue when it is located in root, subfolder A0,A1,B0 but raises an error in subfolder B1. Is error raised is as follows:
Traceback (most recent call last):
File "pose/utils/test.py", line 1, in <module>
import numpy as np
File "/home/adrian/.local/lib/python3.6/site-packages/numpy/__init__.py", line 187, in <module>
from .testing import Tester
File "/home/adrian/.local/lib/python3.6/site-packages/numpy/testing/__init__.py", line 10, in <module>
from unittest import TestCase
File "/usr/lib/python3.6/unittest/__init__.py", line 59, in <module>
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
File "/usr/lib/python3.6/unittest/case.py", line 6, in <module>
import logging
File "/home/adrian/Projects/lpr-pose-estimation/pose/utils/logging.py", line 3, in <module>
from pose.utils.utils import mkdir_if_missing
File "/home/adrian/Projects/lpr-pose-estimation/pose/utils/utils.py", line 4, in <module>
from scipy.misc import imresize
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/misc/__init__.py", line 68, in <module>
from scipy.interpolate._pade import pade as _pade
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/interpolate/__init__.py", line 175, in <module>
from .interpolate import *
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/interpolate/interpolate.py", line 32, in <module>
from .interpnd import _ndim_coords_from_arrays
File "interpnd.pyx", line 1, in init scipy.interpolate.interpnd
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/spatial/__init__.py", line 98, in <module>
from .kdtree import *
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/spatial/kdtree.py", line 8, in <module>
import scipy.sparse
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/sparse/__init__.py", line 230, in <module>
from .base import *
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/sparse/base.py", line 9, in <module>
from scipy._lib._numpy_compat import broadcast_to
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/_lib/_numpy_compat.py", line 16, in <module>
_assert_warns = np.testing.assert_warns
AttributeError: module 'numpy' has no attribute 'testing'
Importing as modules (e.g cv2) also leads to errors, but only wrt to numpy:
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "pose/utils/test.py", line 1, in <module>
import cv2 as cv
File "/home/adrian/.local/lib/python3.6/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: numpy.core.multiarray failed to import
What may be the cause of such an error?
If your start script is located in .../pose/utils then every absolute import looks for modules there, too. This directory contains a module named logging (like the one in the standard library).
During the initialization of the numpy package (executing its __init__.py) and before numpy.testing is available the usual chain of imports happens (as can be seen in the traceback) which leads to the wrong logging module which in turn leads to import of _numpy_compat which tries to access numpy.testing too early.
To avoid this circular import problem you can either rename your logging module or move the start script to another directory.

from requests import Session as OriginalSession ImportError: cannot import name 'Session'

I don't understand the error I get and searching online was not helpful.
Traceback (most recent call last):
File "/Users/<path>/file.py", line 1, in <module>
from coinmarketcap import Market
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coinmarketcap/__init__.py", line 10, in <module>
from .core import Market
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coinmarketcap/core.py", line 8, in <module>
import requests_cache
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests_cache/__init__.py", line 28, in <module>
from .core import(
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests_cache/core.py", line 14, in <module>
from requests import Session as OriginalSession
ImportError: cannot import name 'Session'
It seems to be generated from the very first line where the import and code is the following:
from coinmarketcap import Market
cmc = Market()
coins = cmc.ticker(limit=15) # assumes Dash in top 15.
print(coins)
Not sure if it helps but I had similar issue with import - turned out I have created requests.py file and imported stuff from there. It caused namespace clash as other libraries I used in the project were dependent on built-in requests module.

Test framework quits unexpectedly - IntelliJ python plugin

I'm having a big problem with the testing framework in IntelliJ using with the python plugin.
When I launch my tests, written using unittest, the tests fail with an import error in the _jb_test_runner file.
I surfed the net for a while, but didn't get a useful solution. See the stacktrace below:
/usr/bin/python3.5 /home/davide/.IntelliJIdea2017.1/config/plugins/python/helpers/pycharm/_jb_unittest_runner.py --path /home/davide/Scrivania/musical-store/test/types/test_person.py
Testing started at 12.35 ...
Traceback (most recent call last):
File "/home/davide/.IntelliJIdea2017.1/config/plugins/python/helpers/pycharm/_jb_unittest_runner.py", line 4, in <module>
from unittest import main
File "/usr/lib/python3.5/unittest/__init__.py", line 58, in <module>
from .result import TestResult
File "/usr/lib/python3.5/unittest/result.py", line 5, in <module>
import traceback
File "/usr/lib/python3.5/traceback.py", line 5, in <module>
import linecache
File "/usr/lib/python3.5/linecache.py", line 8, in <module>
import functools
File "/usr/lib/python3.5/functools.py", line 22, in <module>
from types import MappingProxyType
ImportError: cannot import name 'MappingProxyType'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 62, in apport_excepthook
import re, traceback
File "/usr/lib/python3.5/traceback.py", line 5, in <module>
import linecache
File "/usr/lib/python3.5/linecache.py", line 8, in <module>
import functools
File "/usr/lib/python3.5/functools.py", line 22, in <module>
from types import MappingProxyType
ImportError: cannot import name 'MappingProxyType'
Original exception was:
Traceback (most recent call last):
File "/home/davide/.IntelliJIdea2017.1/config/plugins/python/helpers/pycharm/_jb_unittest_runner.py", line 4, in <module>
from unittest import main
File "/usr/lib/python3.5/unittest/__init__.py", line 58, in <module>
from .result import TestResult
File "/usr/lib/python3.5/unittest/result.py", line 5, in <module>
import traceback
File "/usr/lib/python3.5/traceback.py", line 5, in <module>
import linecache
File "/usr/lib/python3.5/linecache.py", line 8, in <module>
import functools
File "/usr/lib/python3.5/functools.py", line 22, in <module>
from types import MappingProxyType
ImportError: cannot import name 'MappingProxyType'
Process finished with exit code 1
The code does not present any syntax error, but the problem started after a refactoring of the whole project I'm working on.
Thanks in advance.

Python request.py syntax error

I am new to Python 3.4.5 which I am learning online by watching videos with some good knowledge of C. I am trying to download an image via Python which I am unable to do because of this error.
Code:
import random
import urllib.request
def img(url):
full='name'+'.jpeg'
urllib.request.urlretrieve(url,full)
img("http://lorempixel.com/400/200")
Error:
Traceback (most recent call last):
File "image.py", line 2, in <module>
import urllib.request
File "/home/yuvi/pyth/urllib/request.py", line 88, in <module>
import http.client
File "/usr/local/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/local/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/local/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/local/lib/python3.4/email/message.py", line 16, in <module>
from email import utils
File "/usr/local/lib/python3.4/email/utils.py", line 31, in <module>
import urllib.parse
File "/home/yuvi/pyth/urllib/parse.py", line 239, in <module>
_DefragResultBase.url.__doc__ = """The URL with no fragment identifier."""
AttributeError: readonly attribute
Try:
def img(url): full='name'+'.jpeg';urllib.urlretrieve(url,full)
urllib.request does not exist in Python 2.x, which seems to be your case
so don't try to import that in second line of your code
plus you made a typo (forgot semicolon) which works as a statement separator while writing inline function statements.
Similar to:
def img(url):
full='name'+'.jpeg'
urllib.urlretrieve(url,full)

Error '__init__ called too many times' while monkey patching

I am using eventlet module. When I try to monkey_patch the code exits with error:
'__init__ called too many times'
Here's the code that's causing problem:
from eventlet import *
patcher.monkey_patch(all=True)
import os, glob, pandas, logging
import numpy as np
import boto3
import datetime
The import works fine, but monkey patching is the step where I see error.
EDIT:
Here's the stack trace:
Traceback (most recent call last):
File "check.py", line 4, in <module>
import os, glob, pandas, logging
File "d:\Python\Anaconda\lib\site-packages\pandas\__init__.py", line 45, in <module>
from pandas.core.api import *
File "d:\Python\Anaconda\lib\site-packages\pandas\core\api.py", line 9, in <module>
from pandas.core.groupby import Grouper
File "d:\Python\Anaconda\lib\site-packages\pandas\core\groupby.py", line 15, in <module>
from pandas.core.frame import DataFrame
File "d:\Python\Anaconda\lib\site-packages\pandas\core\frame.py", line 32, in <module>
from pandas.core.generic import NDFrame, _shared_docs
File "d:\Python\Anaconda\lib\site-packages\pandas\core\generic.py", line 16, in <module>
from pandas.core.internals import BlockManager
File "d:\Python\Anaconda\lib\site-packages\pandas\core\internals.py", line 22, in <module>
from pandas.sparse.array import _maybe_to_sparse, SparseArray
File "d:\Python\Anaconda\lib\site-packages\pandas\sparse\array.py", line 19, in <module>
import pandas.core.ops as ops
File "d:\Python\Anaconda\lib\site-packages\pandas\core\ops.py", line 15, in <module>
import pandas.computation.expressions as expressions
File "d:\Python\Anaconda\lib\site-packages\pandas\computation\expressions.py", line 15, in <module>
import numexpr as ne
File "d:\Python\Anaconda\lib\site-packages\numexpr\__init__.py", line 40, in <module>
from numexpr.expressions import E
File "d:\Python\Anaconda\lib\site-packages\numexpr\expressions.py", line 81, in <module>
_context = Context({})
File "d:\Python\Anaconda\lib\site-packages\numexpr\expressions.py", line 67, in __init__
raise SystemError('__init__ called too many times')
SystemError: __init__ called too many times
Try to run it without all=True:
patcher.monkey_patch()
(you basically have called a class several times)
This is an issue with Windows machines in particular.. patcher.monkey_patch() is working perfectly fine for me in OSX, while the same code returns with -
SystemError: init called too many times for my colleagues using wondows machines ..
I am still searching for a solution for this issue.

Categories