I am a beginner to Python, and I am having trouble importing a list into my other Python file. I have two files. One is article_analyzer.py
and the other is keywords.py
. Both are in the same directory. I have tried importing a list called keywords_list
from keywords.py
. But it is not working. I have tried typing import keywords
and then printing print(keywords_list)
, but this just results in this error: NameError: name 'keywords_list' is not defined
. I have also tried from keywords import *
but it results in the same thing.
Asked
Active
Viewed 112 times
2

EMC
- 91
- 1
- 7
-
Does this answer your question? [Can't import my own modules in Python](https://stackoverflow.com/questions/9383014/cant-import-my-own-modules-in-python) – Björn Apr 17 '20 at 06:13
-
Maybe [this](https://stackoverflow.com/questions/51592182/how-to-import-my-own-modules-the-elegant-way) also helps – Björn Apr 17 '20 at 06:15
-
`import keywords;print(keywords.keywords_list)` may be what you want – tdelaney Apr 17 '20 at 06:23
2 Answers
1
I am not sure what I did, but I played around with the keywords.py
file and it appears to be working now.

EMC
- 91
- 1
- 7
0
you can't import local variable from other file, you can import function, class or global variable.
for example
# keywords.py
def keywords_list:
return ['a', 'b', 'c']
# article_analyzer.py
import keywords
keywords_list = keywords.keywords_list()
print(keywords_list)
# [a, b, c]

Surya Mahadi
- 274
- 1
- 4
-
I don't think this is the problem. It did not work when I tried it. I tried to define a different function in the `keywords.py` folder and I still got the same error. – EMC Apr 17 '20 at 06:35