I am working on a project where I have to write multiple lists, with no way to go around it. I am trying to figure out a way to have another Python file with all these lists so that the main file, which contains the important code, is clean without excessive lines of lists; i.e. main.py
uses lists from lists.py
for the program. Is this possible? If not, is there any other way to go about this situation? Thanks.
Asked
Active
Viewed 48 times
1
-
2`import` is not restricted to built-in modules. – John Coleman Mar 13 '21 at 12:03
3 Answers
2
Just use the python's module import
File 1 in 'parent_folder/data.py':
list_1=[1,2,3]
list_2=[4,5,6]
File 2 in 'parent_folder/main.py':
from data import list_1,list_2
#access items
print('list 1:',list_1)
print('list 2:', list_2)

jeremy302
- 798
- 6
- 10
1
yes you can do this for example:
from yourpythonfile import *
This import everythings. But is better not to do star imports you can also specify the specific variables

Lukas Muijs
- 111
- 4
-
-
-
@buran While that is generally good advice, OP's use-case seems to be one in which it is the desired behavior. They have a module of their own creation and are asking how to bring everything defined there into their main program. Although, even there, it couldn't hurt to be more explicit. – John Coleman Mar 13 '21 at 12:38
-
@JohnColeman, OP wanst to use the variable e.g. `eggs` from the other file `spam.py`. so, what is the problem to do `import spam`, then reference `spam.eggs` when they want to use `eggs`? In this case no start import and is clear what comes from where. I understand they may not want to import each variable individually. – buran Mar 13 '21 at 13:18
0
You can Either
from list import *
print(list_26)
Or
import list
print(list.list_26)
Or
import list as l
print(l.list_26)
First one is the most easiest way and I recommend using from filename import *

EpicPy
- 76
- 1
- 9