In Python, when I use this import statement breze.learn.mlp import iter_minibatches, am getting the following errors.
Here iter_minibatches is a function defined in mlp.py.
Traceback (most recent call last):
File "/home/vinod/PycharmProjects/MLPonTheano/MLPbreze.py", line 15, in <module>
from breze.learn.mlp import Mlp, FastDropoutNetwork
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
from breze.learn.mlp import iter_minibatches
ImportError: cannot import name iter_minibatches
You have a circular import; mlp imports base imports mlp:
# executing mlp.py
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
# executing base.py
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
# this tries to import from mlp again, but mlp isn't done yet
from breze.learn.mlp import iter_minibatches
Any line after the from breze.learn.base import SupervisedModel will not yet have been executed so importing any object defined by those lines will fail.
Avoid circular imports, or if you must have them, delay importing in one of the modules to make sure the objects you need in the other are defined.
Related
I have a problem. I am using Windows 7 32x, Python 3.
When I try to import the numpy library in code, no matter what, I get an error. I don't remember how to solve it.
The code itself:
import numba
print('Hello World!')
And here is the error:
Traceback (most recent call last):
File "C:\Users\User\Desktop\numba_test.py", line 1, in <module>
import numba
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\numba\__init__.py", line 38, in <module>
from numba.core.decorators import (cfunc, generated_jit, jit, njit, stencil,
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\numba\core\decorators.py", line 12, in <module>
from numba.stencils.stencil import stencil
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\numba\stencils\stencil.py", line 11, in <module>
from numba.core import types, typing, utils, ir, config, ir_utils, registry
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\numba\core\registry.py", line 4, in <module>
from numba.core import utils, typing, dispatcher, cpu
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\numba\core\dispatcher.py", line 13, in <module>
from numba.core import (
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\numba\core\compiler.py", line 6, in <module>
from numba.core import (utils, errors, typing, interpreter, bytecode, postproc,
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\numba\core\cpu.py", line 15, in <module>
import numba.core.entrypoints
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\numba\core\entrypoints.py", line 8, in <module>
import importlib_metadata
ValueError: source code string cannot contain null bytes
I have reinstalled numba several times, searched for solutions on the Internet, but all without success. Help what you can.
import numbers
print('Hello World!')
I want to use pandas to process a csv file. The main job is to duplicate a column, so I name the script file as copy.py.
import pandas as pd
df = pd.read_csv('latex.csv')
However, when I execute the file, it gets the error
$ python copy.py
Traceback (most recent call last):
File "~/sourcecode/rime-math/copy.py", line 1, in <module>
import pandas as pd
import pandas as pd
File "~/.local/lib/python3.10/site-packages/pandas/__init__.py", line 50, in <module>
from pandas.core.api import (
File "~/.local/lib/python3.10/site-packages/pandas/core/api.py", line 48, in <module>
from pandas.core.groupby import (
File "~/.local/lib/python3.10/site-packages/pandas/core/groupby/__init__.py", line 1, in <module>
from pandas.core.groupby.generic import (
File "~/.local/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 73, in <module>
from pandas.core.frame import DataFrame
File "~/.local/lib/python3.10/site-packages/pandas/core/frame.py", line 129, in <module>
from pandas.core import (
File "~/.local/lib/python3.10/site-packages/pandas/core/generic.py", line 122, in <module>
from pandas.core.describe import describe_ndframe
File "~/.local/lib/python3.10/site-packages/pandas/core/describe.py", line 37, in <module>
from pandas.core.reshape.concat import concat
File "~/.local/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 45, in <module>
from pandas.core.internals import concatenate_managers
File "~/.local/lib/python3.10/site-packages/pandas/core/internals/__init__.py", line 17, in <module>
from pandas.core.internals.concat import concatenate_managers
File "~/.local/lib/python3.10/site-packages/pandas/core/internals/concat.py", line 3, in <module>
import copy
File "~/sourcecode/rime-math/copy.py", line 4, in <module>
df = pd.read_csv('latex.csv')
AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)
The problem is that the name of your script file is collision with the file one of your module wants to import.
Put attention on the last 5 lines of the traceback since it is near line where error occurs:
File "~/.local/lib/python3.10/site-packages/pandas/core/internals/concat.py", line 3, in <module>
import copy
File "~/sourcecode/rime-math/copy.py", line 4, in <module>
df = pd.read_csv('latex.csv')
AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)
Look at the first 2 lines, it means pandas module need to import copy module at somewhere.
According to The Module Search Path, Python interpreter will search copy module in built-in modules, current directory, PYTHONPATH etc. in order.
Apparently there is no built-in modules named copy in Python, so the interpreter will then look at the files under current directory and find there is copy.py.
The content of copy.py is just one line: df = pd.read_csv(). It means we need to use pandas module. However, we are just on the way importing pandas. Pandas here is only partially initialized, that's why you see that information in backtrace.
To solve this is easy, rename the script file copy.py to some others is ok.
I have been trying to import theano.tensor in my code.
I have used theano.tensor before. All of my previous code importing theano.tensor works perfectly in my machine. Now I am trying to write another script importing the tensor from theano and I am getting the following exception.
Traceback (most recent call last):
File "code.py", line 2, in <module>
import theano.tensor as T
File "/anaconda3/lib/python3.5/site-packages/theano/__init__.py", line 52, in <module>
from theano.gof import (
File "/anaconda3/lib/python3.5/site-packages/theano/gof/__init__.py", line 56, in <module>
from theano.gof.opt import (
File "/anaconda3/lib/python3.5/site-packages/theano/gof/opt.py", line 11, in <module>
import pdb
File "/anaconda3/lib/python3.5/pdb.py", line 75, in <module>
import code
File "/localtmp/saikat/CovInfo/Closure/code.py", line 2, in <module>
import theano.tensor as T
File "/anaconda3/lib/python3.5/site-packages/theano/tensor/__init__.py", line 6, in <module>
from theano.tensor.basic import *
File "/anaconda3/lib/python3.5/site-packages/theano/tensor/basic.py", line 17, in <module>
from theano.tensor import elemwise
File "/anaconda3/lib/python3.5/site-packages/theano/tensor/elemwise.py", line 13, in <module>
from theano import scalar
File "/anaconda3/lib/python3.5/site-packages/theano/scalar/__init__.py", line 2, in <module>
from .basic import *
File "/anaconda3/lib/python3.5/site-packages/theano/scalar/basic.py", line 25, in <module>
from theano import gof, printing
File "/anaconda3/lib/python3.5/site-packages/theano/printing.py", line 22, in <module>
from theano.compile import Function, debugmode, SharedVariable
File "/anaconda3/lib/python3.5/site-packages/theano/compile/__init__.py", line 9, in <module>
from theano.compile.function_module import *
File "/anaconda3/lib/python3.5/site-packages/theano/compile/function_module.py", line 22, in <module>
import theano.compile.mode
File "/anaconda3/lib/python3.5/site-packages/theano/compile/mode.py", line 77, in <module>
OPT_NONE = gof.Query(include=[], exclude=exclude)
AttributeError: module 'theano.gof' has no attribute 'Query'
I cannot find any plausible reason for this exception.
I guess i got your problem. See in the error log:
File "/anaconda3/lib/python3.5/pdb.py", line 75, in <module>
import code
I believe there is another script called code.py in Theano which gets called from pdb.py when python interpreter executes your script which is also named as code.py. I am guessing python interpreter is mixing up these two scripts and executing the wrong one! You can change the filename and check whether the error disappear or not.
I've encountered a very strange error when trying to Import mayavi. A few minutes ago it worked like a charm, but now there's something going wrong and I've no clue why.
I start python from the terminal and then I type:
from mayavi import mlab
The error I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/mayavi/mlab.py", line 27, in <module>
from mayavi.tools.camera import view, roll, yaw, pitch, move
File "/usr/lib/python2.7/dist-packages/mayavi/tools/camera.py", line 23, in <module>
from engine_manager import get_engine
File "/usr/lib/python2.7/dist-packages/mayavi/tools/engine_manager.py", line 14, in <module>
from mayavi.core.engine import Engine
File "/usr/lib/python2.7/dist-packages/mayavi/core/engine.py", line 28, in <module>
from mayavi.core.scene import Scene
File "/usr/lib/python2.7/dist-packages/mayavi/core/scene.py", line 15, in <module>
from mayavi.core.source import Source
File "/usr/lib/python2.7/dist-packages/mayavi/core/source.py", line 19, in <module>
from mayavi.core.module_manager import ModuleManager
File "/usr/lib/python2.7/dist-packages/mayavi/core/module_manager.py", line 19, in <module>
from mayavi.core.lut_manager import LUTManager
File "/usr/lib/python2.7/dist-packages/mayavi/core/lut_manager.py", line 10, in <module>
import subprocess
File "/usr/lib/python2.7/subprocess.py", line 432, in <module>
import pickle
File "pickle.py", line 4, in <module>
from mayavi import mlab
ImportError: cannot import name mlab
What's wrong with Python?
You have a local file named pickle.py; this is being imported instead of the pickle module. This module then tries to import mlab before that module itself has completed importing in a circular import dependency:
File "/usr/lib/python2.7/subprocess.py", line 432, in <module>
import pickle
File "pickle.py", line 4, in <module>
from mayavi import mlab
Note how subprocess tries to import pickle and finds your file instead (the path is relative instead of a full path inside /usr/lib/python2.7/.
Rename pickle.py to something else, you are masking the standard library here.
I have a script which requires multiprocessing. What I found from this script is that there is a problem with the multiprocessing module. To test this theory, I copied and pasted
from multiprocessing import Process
def f(name):
print('hello', name)
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
into a test script and received the following traceback
Traceback (most recent call last):
File "a.py", line 1, in <module>
from multiprocessing import Process
File "/usr/lib64/python3.3/multiprocessing/__init__.py", line 40, in <module>
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/usr/lib64/python3.3/multiprocessing/util.py", line 16, in <module>
import threading # we want threading to install it's
File "/usr/lib64/python3.3/threading.py", line 11, in <module>
from traceback import format_exc as _format_exc
File "/usr/lib64/python3.3/traceback.py", line 3, in <module>
import linecache
File "/usr/lib64/python3.3/linecache.py", line 10, in <module>
import tokenize
File "/usr/lib64/python3.3/tokenize.py", line 30, in <module>
from token import *
File "/home/lucas/Server/ClinApp/weblabs/utils/token.py", line 1, in <module>
from django.conf import settings
File "/usr/lib/python3.3/site-packages/django/conf/__init__.py", line 9, in <module>
import logging
File "/usr/lib64/python3.3/logging/__init__.py", line 195, in <module>
_lock = threading.RLock()
AttributeError: 'module' object has no attribute 'RLock'
Also, I am running fedora 18 64-bit on a quad core ivy bridge. Why am I receiving this traceback error?
Suggestion
Here is what happens when I run RLock
$ python3
>>> import threading
>>> threading.RLock()
<_thread.RLock owner=0 count=0>
>>>
File "/usr/lib64/python3.3/tokenize.py", line 30, in <module>
from token import *
File "/home/lucas/Server/ClinApp/weblabs/utils/token.py", line 1, in <module>
from django.conf import settings
Your /home/lucas/Server/ClinApp/weblabs/utils/token.py script is being imported instead of the standard python 'token.py'. Its got a bug in it or it simply shouldn.t be imported as a top level script. You probably have /home/lucas/Server/ClinApp/weblabs/utils/ in your python path in some way.
Generally, its not a good idea to name python scripts after builtin scripts.
After you rename token.py to something else (get_token.py), remember to delete token.pyc in your local working directory. Or else, you will continue to get the Traceback error message you listed above.