Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import list variable from separate files in python

Tags:

python

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?

like image 241
O P Avatar asked Mar 25 '15 21:03

O P


People also ask

Can I import a list from a Python file?

Yes! Simply create another python list and import it in the main program. Save this answer.

What does __ import __ do in Python?

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.

How do you fix a circular import error in Python?

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.


1 Answers

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
like image 144
Justin Andrew Avatar answered Sep 28 '22 07:09

Justin Andrew