Importing Definitions

Chris Angelico rosuav at gmail.com
Thu Sep 5 08:50:35 EDT 2013


On Thu, Sep 5, 2013 at 10:39 PM, Azureaus <lo0446 at my.bristol.ac.uk> wrote:
> Lets say I have some definitions in a module1.py e.g.
>
> import sys
> A,B,C,D,E = range(5)
> def method1():
> more code
> end
>
> Then a second module module2.py where I wish to use these definitions
> import module1.py
> print A
>
> This will throw an error saying "global name 'A' is not defined."
>
> Now I know if there was a method I wanted to reference I could do something like this in module2.
> from module1 import method1
>
> which would mean from that point on I could just reference it as method1 rather than module1.method1, is there such a way I could do this with definitions??

You can! Any name will work, functions aren't special.

from module1 import method1, A, B, C, D, E

If you want all of them at once, you may want:

from module1 import *

but this can make your code confusing.

ChrisA



More information about the Python-list mailing list