Skip to content Skip to sidebar Skip to footer

I Want To Refer To A Variable In Another Python Script

A variable AA is in aaa.py. I want to use this variable in my other python file bbb.py How do I access this variable?

Solution 1:

You're looking for modules!

In aaa.py:

AA = 'Foo'

In bbb.py:

import aaa
print aaa.AA # Or print(aaa.AA) for Python 3# Prints Foo

Or this works as well:

from aaa import AA
print AA
# Prints Foo

Solution 2:

You can import it; this will execute the whole script though.

from aaa importAA

Solution 3:

In your file bbb.py, add the following:

import sys
sys.path.append("/path/to/aaa.py/folder/")
from aaa import AA

Also would suggest reading more about Python modules and how import works. Official Documentation on Modules.

Solution 4:

Although importing is the best approach (like poke or Haidro wrote), here is another workaround, if you're generating data with one script and want to access them in another, without executing "bbb.py". So if you're dealing with large lists/dicts, this approach works well, although it's an overkill if you simply trying to interchange a string or a number…

Besides that, you should describe your problem more detailed, since importing variables from another script is kind of hacky and may not be the way to go.

So here are two functions, the first one (dumpobj) dumps a variable (string, number, tuple, list, dict sets whatever) to a file, the second one (loadobj) reads it in. I decided to use json, since the data files are human readable.

You may want to try that, if you need it often:

import json


defdumpobj(the_object, filename) :
    """
    Dumps the_object to filename via json. Use loadobj(filename)
    to get it back.

         >>> from tgio import dumpobj, loadobj
         >>> foo = {'bar':42, 'narf':'fjoord', 23:[1,2,3,4,5]}
         >>> dumpobj(foo, 'foo.var')
         >>> bar = loadobj('foo.var')
         >>> bar
         {u'narf': u'fjoord', u'bar': 42, u'23': [1, 2, 3, 4, 5]}
    """try:
        withopen(filename):
            print(filename + " exists, I'll will overwrite it.")
    except IOError:
        print(filename + ' does not exist. Creating it...')

    f = open(filename, 'w')
    json.dump(the_object, f)
    f.close()

defloadobj(filename) :
    """
    Retrieves dumped data (via json - see dumpobj) from filename.

         >>> from tgio import loadobj, dumpobj
         >>> foo = {'bar':42, 'narf':'fjoord', 23:[1,2,3,4,5]}
         >>> dumpobj(foo, 'foo.var')
         >>> bar = loadobj('foo.var')
         >>> bar
         {u'narf': u'fjoord', u'bar': 42, u'23': [1, 2, 3, 4, 5]}
    """try:
        withopen(filename):
            print("Reading object from file " + filename)
    except IOError:
        print(filename + ' does not exist. Returning None.')
        returnNone

    f = open(filename, 'r')
    the_object = json.load(f)
    f.close
    return the_object

See the usage examples in the docstrings!

Post a Comment for "I Want To Refer To A Variable In Another Python Script"