45

I'm new and trying to make a simple random sentence generator- How can I pull a random item out of a list that is stored in another .py document? I'm using

random.choice(verb_list) 

to pull from the list. How do I tell python that verb_list is in another document?

Also it would be helpful to know what the principle behind the solution is called. I imagine it's something like 'file referencing' 'file bridging' etc..

1
  • Added the python-import tag, in the hopes that it will help future duplicate-closers to find this canonical. Commented Jul 27, 2022 at 20:13

2 Answers 2

87

You can import the variables from the file:

vardata.py

verb_list = [x, y, z]
other_list = [1, 2, 3]
something_else = False

mainfile.py

from vardata import verb_list, other_list
import random

print random.choice(verb_list) 

you can also do:

from vardata import *

to import everything from that file. Be careful with this though. You don't want to have name collisions.

Alternatively, you can just import the file and access the variables though its namespace:

import vardata
print vardata.something_else
Sign up to request clarification or add additional context in comments.

3 Comments

this does not work if the variable you need imported is defined in a function. How do you get around this?
@gmonz try returning the variable from your function and import everything
@gmonz That is by design via function scoping rules. Any variables inside a function should be returned if you need to access them elsewhere. You can access the returned output by calling the function from the other file after importing it.
10

It’s called importing.

If this is data.py:

verb_list = [
    'run',
    'walk',
    'skip',
]

and this is foo.py:

#!/usr/bin/env python2.7

import data
print data.verb_list

Then running foo.py will access verb_list from data.py.


You might want to work through the Modules section of the Python tutorial.


If verb_list is stored in a script that you want to do other things too, then you will run into a problem where the script runs when all you’d like to do is import its variables. In that case the standard thing to do is to keep all of the script functionality in a function called main(), and then use a magic incantation:

verb_list = [
    'run',
    'walk',
    'skip',
]

def main():
    print 'The verbs are', verb_list

if __name__ == '__main__':
    main()

Now the code in main() won’t run if all you do is import data. If you’re interested, Python creator Guido van Rossum has written an article on writing more elaborate Python main() functions.

3 Comments

Note that one doesn't have to put that code in a main() function, it can be just put in the if clause.
True, but this way other modules can also access the main functionality of scripts when importing. I’ve updated my answer to link to an article with further elaborations.
Thank you- using main is what did the trick. I had been using import but like you said, my verb_list script would run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.