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.
Related
My code is:
import re
s="An apple in a day."
print(re.search("in",s))
Error:
C:\Users\DELL\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/DELL/AppData/Local/Programs/Python/Python38-32/Lib/encodings/re.py
Traceback (most recent call last):
File "C:/Users/DELL/AppData/Local/Programs/Python/Python38-32/Lib/encodings/re.py", line 1, in <module>
import re
File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\Lib\encodings\re.py", line 3, in <module>
re.search("is",s)
AttributeError: partially initialized module 're' has no attribute 'search' (most likely due to a circular import)
Process finished with exit code 1
You name your file re.py so python confused which one to use when you call import re: your file or regexp library.
Try to rename your file to something else and not to choose other library names.
Cant set the process in Python 2.7.17 pwntools.
Source code:
from pwn import *
s=process('/root/Dokumente/Scripts/example_program')
I tried from pwn import *:
root#bitpc:~# python pwn.py
Traceback (most recent call last):
File "pwn.py", line 1, in <module>
from pwn import *
File "/root/pwn.py", line 2, in <module>
s=process('/root/Dokumente/Scripts/example_program')
NameError: name 'process' is not defined
That was not working. Then i imported process directly:
root#bitpc:~# python pwn.py
Traceback (most recent call last):
File "pwn.py", line 1, in <module>
from pwn import process
File "/root/pwn.py", line 1, in <module>
from pwn import process
ImportError: cannot import name process
I got an import error. How to fix this?
It seems that your exploit script's name is pwn.py. rename it to some other name such as exp.py. otherwise python will try to import things in your pwn.py instead of importing things from pwntools.
during typing a code on the python shell for an optimization problem i got this error for unknown reasons GurobiError: Variable has not yet been added to the modeland this is the part of the code:
from gurobipy import *
>>> m=Model("scanning")
>>> from sympy import *
>>> v=zeros(3,1)
>>> ub=zeros(3,1)
>>> lb=zeros(3,1)
>>> x=m.addVar(lb=0,ub=3,vtype='I',name='x')
>>> m.addConstr(Array([[1,2,3],[2,1,0],[2,3,1]])*x>=0,"c0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/midow/anaconda3/lib/python3.6/site-packages/sympy/tensor/array/ndim_array.py", line 346, in __mul__
other = sympify(other)
File "/Users/midow/anaconda3/lib/python3.6/site-packages/sympy/core/sympify.py", line 266, in sympify
if a in sympy_classes:
File "var.pxi", line 39, in gurobipy.Var.__hash__
gurobipy.GurobiError: Variable has not yet been added to the model
i don't know why am i getting this error despite that syntax is right i guess, i don't know if there's something missing.
i am using Python 3.6.8on macOS high sierra.
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?
I am trying to input a matix using numpy and I get the following error:
Traceback (most recent call last):
File "phase1.py", line 1, in <module>
import numpy as np
File "/home/rockstar/phase1.py", line 2, in <module>
a= np.array([1,2,3])
AttributeError: 'module' object has no attribute 'array'
I am not sure why this error is there. Could anyone help?
You probably have a file called numpy.py in the same folder that shadows the real numpy module. Rename your .py file and delete its .pyc file.