227

How can I import variables from one file to another?

example: file1 has the variables x1 and x2 how to pass them to file2?

How can I import all of the variables from one to another?

martineau
  • 119,623
  • 25
  • 170
  • 301
Some kid
  • 2,417
  • 3
  • 14
  • 12
  • *Why* do you want to do that? Depending on the use-case, this is actually not what you really want. – Mayou36 Sep 01 '21 at 11:48

8 Answers8

231
from file1 import *  

will import all objects and methods in file1

karthikr
  • 97,368
  • 26
  • 197
  • 188
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
  • 84
    Do note, however, this is generally something you [should not do](http://docs.python.org/2/howto/doanddont.html#from-module-import). – David Cain Jun 22 '13 at 22:17
  • 11
    As David notes, this pollutes the namespace and can have catastrophic consequences by masking objects and functions from other modules including those in the standard distro – ennuikiller Jun 22 '13 at 22:27
  • 15
    I tried to do it, but it seems like python can't find the *file1*, because I'm getting this error: `ImportError: No module named file1` – Hilder Vitor Lima Pereira May 29 '15 at 23:52
  • 6
    Did you have a `__init__.py` file? See [here](http://stackoverflow.com/questions/4142151/python-how-to-import-the-class-within-the-same-directory-or-sub-directory) for more details. – J0ANMM Sep 28 '16 at 07:21
  • Prepending a . at the beginning of file1 worked for me. i.e ```from .file1 import *``` – Jan Ndungu Jun 27 '22 at 18:24
127

Import file1 inside file2:

To import all variables from file1 without flooding file2's namespace, use:

import file1

#now use file1.x1, file2.x2, ... to access those variables

To import all variables from file1 to file2's namespace( not recommended):

from file1 import *
#now use x1, x2..

From the docs:

While it is valid to use from module import * at module level it is usually a bad idea. For one, this loses an important property Python otherwise has — you can know where each toplevel name is defined by a simple “search” function in your favourite editor. You also open yourself to trouble in the future, if some module grows additional functions or classes.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
75

Best to import x1 and x2 explicitly:

from file1 import x1, x2

This allows you to avoid unnecessary namespace conflicts with variables and functions from file1 while working in file2.

But if you really want, you can import all the variables:

from file1 import * 
Chris Redford
  • 16,982
  • 21
  • 89
  • 109
31

Actually this is not really the same to import a variable with:

from file1 import x1
print(x1)

and

import file1
print(file1.x1)

Altough at import time x1 and file1.x1 have the same value, they are not the same variables. For instance, call a function in file1 that modifies x1 and then try to print the variable from the main file: you will not see the modified value.

Marc Rechté
  • 1,817
  • 1
  • 13
  • 5
16

first.py:

a=5

second.py:

import first
print(first.a)

The result will be 5.

陳仲肯
  • 186
  • 1
  • 9
12

script1.py

title="Hello world"

script2.py is where we using script1 variable

Method 1:

import script1
print(script1.title)

Method 2:

from script1 import title
print(title)
Ravi
  • 2,778
  • 2
  • 20
  • 32
9

Marc response is correct. Actually, you can print the memory address for the variables print(hex(id(libvar)) and you can see the addresses are different.

# mylib.py
libvar = None
def lib_method():
    global libvar
    print(hex(id(libvar)))

# myapp.py
from mylib import libvar, lib_method
import mylib

lib_method()
print(hex(id(libvar)))
print(hex(id(mylib.libvar)))
VictorGalisson
  • 635
  • 9
  • 27
Lei Z
  • 163
  • 2
  • 1
0

if you need to import of a variable from a dir on the same level or below you can use "import_module" coming from you cwd of the project:

        from importlib import import_module
        mod = import_module(
            f"{cwd}.source.myfolder.myfile"
        )
        var = getattr(mod, "my_variable")
Manute
  • 83
  • 3