script.py
list = ['one',
'two',
'three']
# python script here
My list is getting really long (about 400 lines)... Is there a way to have the variable list
in a separate py file and import it?
Or should I have separate file and create a list by iterating over it?
list.txt
one
two
three
How can I achieve this?
Yes! Simply create another python list and import it in the main program. Save this answer.
The __import__() in python module helps in getting the code present in another module by either importing the function or code or file using the import in python method. The import in python returns the object or module that we specified while using the import module.
If the error occurs due to a circular dependency, it can be resolved by moving the imported classes to a third file and importing them from this file. If the error occurs due to a misspelled name, the name of the class in the Python file should be verified and corrected.
Yes! Simply create another python list and import it in the main program.
in variables.py
mylist = ['this', 'that', 'theother']
in main.py
from variables import mylist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With