Suppose I do this
import cmath
del cmath
cmath.sqrt(-1)
I get this
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'cmath' is not defined
But when I import cmath again, I am able to use sqrt again
import cmath
cmath.sqrt(-1)
1j
But when I do the following
import cmath
del cmath.sqrt
cmath.sqrt(-1)
I get this
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'sqrt'
Even when I import cmath again, I get the same error.
Is it possible to get cmath.sqrt back?
Thanks!
You'd need reload
reload(cmath)
... will reload definitions from the module.
import cmath
del cmath.sqrt
reload(cmath)
cmath.sqrt(-1)
... will correctly print ..
1j
Related
I am working with Python's decimal module, but when I do:
>>>from decimal import Decimal
>>>d = Decimal('3.14')
>>>d.__slots__
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'decimal.Decimal' object has no attribute '__slots__'
>>>Decimal.__slots__
raceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: type object 'decimal.Decimal' has no attribute '__slots__'
But when I inspect Decimal class in PyCharm (_pydecimal.py), I see:
class Decimal(object):
"""Floating point class for decimal arithmetic."""
__slots__ = ('_exp','_int','_sign', '_is_special')
...
Supposedly, I should be able to get __slots__ from class/instance, but I didn't. What am I missing here? (This kinda behaves like Python's built-in int class.)
If you look at decimal.py, you'll see this:
try:
from _decimal import *
from _decimal import __doc__
from _decimal import __version__
from _decimal import __libmpdec_version__
except ImportError:
from _pydecimal import *
from _pydecimal import __doc__
from _pydecimal import __version__
from _pydecimal import __libmpdec_version__
It first tries to import from _decimal, which is a precompiled shared library on my system. The code you found via PyCharm is instead in the fallback _pydecimal implementation. I suspect your code is in fact loading the _decimal version of Decimal, which simply doesn't use __slots__.
I have tried following
when I tried to import the class from the file named plyparser.py of pycparser(https://github.com/eliben/pycparser/blob/master/pycparser/plyparser.py)
using the following statement
from pycparser.plyparser import Coord
But, I am unable to import function parameterized from plyparser of pycparser using the following statement
from pycparser.plyparser import parameterized
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name parameterized
How to resolve this issue.
I can import matplotlib but when I try to run the following:
matplotlib.pyplot(x)
I get:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
matplotlib.pyplot(x)
AttributeError: 'module' object has no attribute 'pyplot'
pyplot is a sub-module of matplotlib which doesn't get imported with a simple import matplotlib.
>>> import matplotlib
>>> print matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'pyplot'
>>> import matplotlib.pyplot
>>>
It seems customary to do: import matplotlib.pyplot as plt at which time you can use the various functions and classes it contains:
p = plt.plot(...)
Did you import it? Importing matplotlib is not enough.
>>> import matplotlib
>>> matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'pyplot'
but
>>> import matplotlib.pyplot
>>> matplotlib.pyplot
works.
pyplot is a submodule of matplotlib and not immediately imported when you import matplotlib.
The most common form of importing pyplot is
import matplotlib.pyplot as plt
Thus, your statements won't be too long, e.g.
plt.plot([1,2,3,4,5])
instead of
matplotlib.pyplot.plot([1,2,3,4,5])
And: pyplot is not a function, it's a module! So don't call it, use the functions defined inside this module instead. See my example above
Let's say A is a package directory, B is a module within the directory, and X is a function or variable written in B. How can I import X using the __import__() syntax? Using scipy as an example:
What I want:
from scipy.constants.constants import yotta
What doesn't work:
>>> __import__("yotta", fromlist="scipy.constants.constants")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named yotta
>>> __import__("yotta", fromlist=["scipy.constants.constants"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named yotta
>>> __import__("yotta", fromlist=["scipy","constants","constants"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named yotta
>>> __import__("scipy.constants.constants.yotta", fromlist=["scipy.constants.constats"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named yotta
Any suggestions would be much appreciated.
The python import statement performs two tasks: loading the module and makeing it available in the namespace.
import foo.bar.baz
will provide the name foo in the namespace, not baz, so __import__ will give you foo
foo = __import__('foo.bar.baz')
On the other hand
from foo.bar.baz import a, b
does not make a module available, but what the import statement needs to perform the assignmaents is baz. this corresponds to
_tmp_baz = __import__('foo.bar.baz', fromlist=['a', 'b'])
a = _tmp_baz.a
b = _tmp_baz.b
without making the temporary visible, of course.
the __import__ function does not enforce the presence of a and b, so when you want baz you can just give anything in the fromlist argument to put __import__ in the "from input" mode.
So the solution is the following. Assuming 'yotta' is given as a string variable, I have used getattr for attribute access.
yotta = getattr(__import__('scipy.constants.constants',
fromlist=['yotta']),
'yotta')
__import__("scipy.constants.constants", fromlist=["yotta"])
The argument fromlist is equivalent to the right hand side of from LHS import RHS.
From the docs:
__import__(name[, globals[, locals[, fromlist[, level]]]])
[...]
The fromlist gives the names of objects or submodules that should be imported from the module given by name.
[...]
On the other hand, the statement from spam.ham import eggs, sausage as saus results in
_temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], -1)
eggs = _temp.eggs
saus = _temp.sausage
(Emphasis mine.)
I am trying to read a *.wav file using scipy. I do the following:
import scipy
x = scipy.io.wavfile.read('/usr/share/sounds/purple/receive.wav')
As a result of this code I get:
Traceback (most recent call last):
File "test3.py", line 2, in <module>
x = scipy.io.wavfile.read('/usr/share/sounds/purple/receive.wav')
AttributeError: 'module' object has no attribute 'io'
Does anybody know what is wrong here? Thank you in advance.
As the error says, scipy module does not have 'io'.
io.wavfile is a submodule, you need to from scipy.io import wavfile and then do wavfile.read("/usr/share/sounds/purple/receive.wav")
This gives me an error with the file you are using as an example, however...