I tried to use the Z3Py dll, but it didn't work. Here are my test programs and errors. I am very new to Python, I think I missed some important part everybody already knows.
init("z3.dll")
Traceback (most recent call last):
File "test5.py", line 1, in <module>
init("z3.dll")
NameError: name 'init' is not defined
I also tried another way to load the dll:
import ctypes
so = ctypes.WinDLL('./z3.dll') #for windows
print(so)
s = Solver()
<WinDLL './z3.dll', handle 10000000 at 0x10b15f0>
Traceback (most recent call last):
File "test5.py", line 5, in <module>
s = Solver()
NameError: name 'Solver' is not defined
Typically, all you have to do is import z3:
from z3 import *
s = Solver()
x = Int("x")
s.add(x > 5)
s.check()
print s.model()
What happens when you run this simple script?
Related
So I've started to learn Python recently and right now I've been trying to learn arrays, but unable to use the array function after importing the array library.
I have tried four different methods to use the array function but failed successfully.
Method 1:
import array
nums = array.array('i', [])
#rest of the code
Output 1:
Traceback (most recent call last):
File "array.py", line 2, in <module>
import array
File "/home/prince/Desktop/python-basics/array.py", line
4, in <module>
nums = array.array('i', [])
TypeError: 'module' object is not callable
Method 2:
import array as a
nums = a.array('i', [])
#rest of the code
Output 2:
Traceback (most recent call last):
File "array.py", line 2, in <module>
import array as a
File "/home/prince/Desktop/python-basics/array.py", line
4, in <module>
nums = a.array('i', [])
AttributeError: partially initialized module 'array' has
no attribute 'array' (most likely due to a circular
import)
Method 3:
from array import array
nums = array('i', [])
#rest of the code
Output 3:
Traceback (most recent call last):
File "array.py", line 2, in <module>
from array import array
File "/home/prince/Desktop/python-basics/array.py", line
2, in <module>
from array import array
ImportError: cannot import name 'array' from partially
initialized module 'array' (most likely due to a circular
import) (/home/prince/Desktop/python-basics/array.py)
Method 4:
from array import *
nums = array('i', [])
Output 4:
Traceback (most recent call last):
File "array.py", line 2, in <module>
from array import *
File "/home/prince/Desktop/python-basics/array.py", line
4, in <module>
nums = array('i', [])
NameError: name 'array' is not defined
And after compilation, every time another folder is automatically created in my directory whose name is : pycache
And inside that folder there is a file named: array.cpython-38.pyc which I am unable to open. My editor says that it is because it either uses binary or unsupported text.
A few additional details if that helps:
Text Editor I Used: VS Code
My OS: Ubuntu 20.04LTS
Python Version: 3.8.5
All of the above imports fail due to the file name being same as the module name which you import. Pretty sure you can't have the same name as the module you're trying to import. Try renaming the filename array.py to something else and it should work.
About pycache folder, it contains the compiled bytecode for the python program. This shouldn't have anything to do with this problem.
In Python 3, when I import a module using exec in the global scope, it works. But when I do it within a function, even though I get no import error, Python does not recognize the module name.
Importing and using sys successfully, in the global scope:
>>> sys.argv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> exec('import sys')
>>> sys.argv
['']
No import error, though cannot use os which has been imported from a function:
>>> def import_os():
... exec('import os')
... os.listdir('.')
...
>>> import_os()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in import_os
NameError: name 'os' is not defined
>>>
Any idea how to make this work from within a function?
I am trying to generate the help text at runtime and i am not able to use the pydoc command in Windows. When i type
>>> pydoc(atexit)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'pydoc' is not defined
I have already set up the environment variables for pydoc.py file. C:\Python33\Lib\pydoc.py.
This also not works like it works for >>help('atexit')
>>> pydoc('atexit')
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'pydoc' is not defined
Whats the possible reason for it.
Updates:
>>> import pydoc
>>> pydoc(sys)
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'module' object is not callable
>>> pydoc('sys')
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'module' object is not callable
Like any library in Python, you need to import it before you can use it.
Edit What exactly are you trying to achieve? Modules are indeed not callable. pydoc.help is the function you want, although I don't really know why you need it, since as you note the standalone help function does the same thing already.
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 would like to know why
>>> def func2():
... global time
... import time
...
>>> time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'time' is not defined
>>> func2()
>>> time
<module 'time' (built-in)>
>>>
works, but
>>> def func():
... global module
... module="time"
... exec ("global %s" %module)
... exec ("import %s" %module)
...
>>> time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'time' is not defined
>>> func()
>>> time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'time' is not defined
works not, and how i could get it to work =)
thank you
Each of your exec() calls happens in a separate namespace. Abandon this path; it will only lead to ruin.
Because exec uses its own scope by default. If you do exec "global {0}; import {0}".format(module) in globals(), then it'll work.
You shouldn't be doing that, unless you really need to.
To import a module given the name as a string use
time=__import__('time')
Here's one way you might use it
usermodulenames = ["foo","bar","baz"]
usermodules = dict((k,__import__(k)) for k in usermodulenames)
What you are trying to do is either very sophisticated or very odd. This is how it works:
exec ("import %s" % module) in globals()
Please describe the bigger problem you are trying to solve