i have a this 2 file: main.py and abc.py
main.py is:
dm = [100, 200, 300]
import abc
abc.abcp(dm)
from abc import *
pabc = abc.dmabc
print pabc
abc.py is:
def abcp(dm):
dmabc = list(dm)
dmabc[0] -= 50
print dmabc
return dmabc
The error is: pabc = abc.dmabc (AttributeError: 'module' object has no attribute dmabc)
if i write:
from abc import abcp
pabc = abc.dmabc
print pabc
The error is: from abc import abcp (ImportError: cannot import name bmf)
if i write:
from abc import abcp
from abcp import dmabc
pabc = abc.dmabc
print pabc
The error is: from abpc import dmabc (ImportError: No module named abpc)
So how can i import dmabc variable from abc.py file?
1 Answer 1
You cannot access the variable dmabc because it's a local variable in abc.py.
The best way would be to store the return value when calling abc.dmabc:
main.py:
dm = [100, 200, 300]
import abc
pabc = abc.abcp(dm)
print pabc
answered May 29, 2015 at 9:29
Leistungsabfall
6,4987 gold badges38 silver badges44 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Nicola Currò
So i have to print in that file instead try to import variable?
Leistungsabfall
You can also print it inside
abc.py, but the only way to access it in main.py would be to store the return value of the method. So, printing works in both modules but importing does not work since it's a local variable that only exists in abc.py and you cannot access it from main.py.lang-py
dmabconly exists insideabcp, so you can't access it from other modules. If you need to, do something likepabc = abc.abcp(dm).