from file1 import *  

will import all objects and methods in file1

Answer from ennuikiller on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-import-variables-from-another-file-in-python
How to import variables from another file in Python? - GeeksforGeeks
July 23, 2025 - To import variables from another file in Python, you need to use the import statement. By importing a file, you gain access to its variables and functions. This can be done by simply using import filename or from filename import variable_name ...
Discussions

Importing a variable from another python file imports everything?
When you import a file, that file gets run. The only difference between import file and from file import name, is that you can then access name directly, as opposed to file.name. For this reason it is important that imported files check to see if __name__ == ' __main__', before executing any code, like this: names = ['Jack', 'Daniels', 'Johnnie', 'Walker'] print('i am running') # prints on import def main(): # some code here to run when the file is run directly and not imported if __name__ == '__main__': main() More on reddit.com
🌐 r/learnpython
5
0
February 17, 2022
How would I import variables from one python file to another when they are in two different folders but in the same directory?
When you run a Python script, you can't import anything that's not either a site package or underneath the enclosing folder. You can't go "up and over" and import. More on reddit.com
🌐 r/learnpython
5
1
December 14, 2023
python import as a variable name - Stack Overflow
I wanted to use import with a variable name. For example I wanted to do something like this from var import my_class I went through pythons documentation, but seems thats a little confusing. Also ... More on stackoverflow.com
🌐 stackoverflow.com
How to import variable from another file if another file write the code inside def main():
The whole purpose of a function is to enclose local variables and not expose them to the outside. If you want a function to have some value from main, you pass that value as an argument when you call that function. def main(): v = 1 otherfunc(v) def otherfunc(v): #now otherfunc has access to v's value print(v) More on reddit.com
🌐 r/learnpython
11
0
March 26, 2022
🌐
Dive into Python
diveintopython.org › home › learn python programming › variables in python › how to import variables from files
Examples of Import Variables and Exporting Strings to Files in Python
May 3, 2024 - Suppose you have a Python file named file1.py which contains a variable named my_variable. # file2.py from file1 import my_variable # Now you can use the variable in this file print(my_variable)
🌐
CodeBurst
codeburst.io › importing-variables-from-other-files-in-python-96a200f410da
Importing variables from other files in Python | by aneesh joshi | codeburst
June 30, 2017 - from A import aprint('Imported value of a is %s' % str(a))print('value of __name__ in B is %s' % __name__) ... └> python B.py the value of __name__ in A is A Imported value of a is 0 value of __name__ in B is __main__
🌐
Medium
medium.com › @gl7526 › importing-variables-from-another-file-5afcdaa6c8df
Importing Variables From Another File | by George | Medium
November 17, 2020 - The simplest way to do so is to have a regular Python file that you want to import variables from, and the file you want to import the variable to, in the same directory.
🌐
EyeHunts
tutorial.eyehunts.com › home › python import variable from another file | example code
Python import variable from another file | Example code
August 1, 2022 - There are three approaches to importing variables from another file. import <file> and then use <file_name>.<variable_name> to access variable
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › importing a variable from another python file imports everything?
r/learnpython on Reddit: Importing a variable from another python file imports everything?
February 17, 2022 -

Hi guys, I am currently working on some assignments from Python Crash course book for self study, however, I am trying to import a specific list from a few files and this leads to the running of all previous code, what am I doing wrong?

Here are the links to the series of python files in chronological order:

  1. https://pastebin.com/zqpp3Auv

  2. https://pastebin.com/9PxNzBM7

  3. https://pastebin.com/rjeapb8J

Here is the resulting output of the third python file: https://pastebin.com/zT6Nvu5V

What am I doing wrong?

🌐
Python Forum
python-forum.io › thread-23800.html
Importing variables from another file
January 17, 2020 - I've been googling this problem for a while now and nothing seems to work. Here's my problem: I have variables in 4 different files. I want these variables to be read by my main file. I started programming today, so I thought a simple text based c...
Top answer
1 of 2
8

Here's how to use importlib (there is no need for the second parameter):

var = importlib.import_module("var")

# Now, you can use the content of the module:
var.my_class()

There is no direct programmable equivalent for from var import my_class.

2 of 2
-2

Note: As @DYZ points out in the comments, this way of solving this is not recommended in favor of importlib. Leaving it here for the sake of another working solution, but the Python docs advise "Direct use of import() is also discouraged in favor of importlib.import_module()."

Do you mean that you want to import a module whose name is defined by a variable? If so, you can use the __import__ method. For example:

>>> import os
>>> os.getcwd()
'/Users/christophershroba'
>>>
>>> name_to_import = "os"
>>> variable_module = __import__(name_to_import)
>>> variable_module.getcwd()
'/Users/christophershroba'

If you also want to call a variable method of that variable module you could use the __getattribute__ method on the module to get the function, and then call it with () as normal. The line below marked "See note" is not necessary, I just include it to show that the __getattribute__ method is returning a function.

>>> name_to_import = "os"
>>> method_to_call = "getcwd"
>>> variable_module = __import__(name_to_import)
>>> variable_module.__getattribute__(method_to_call) # See note
<built-in function getcwd>
>>> variable_module.__getattribute__(method_to_call)()
'/Users/christophershroba'

More documentation available for Python 3 here or Python2 here.

🌐
pythontutorials
pythontutorials.net › blog › importing-and-changing-variables-from-another-file
How to Import and Change Variables from Another File in Python: A Beginner's Guide — pythontutorials.net
As you progress in Python, you’ll quickly realize that writing all your code in a single file becomes messy—especially for larger projects. Splitting code into separate files (called modules) helps organize logic, reuse code, and keep your projects scalable. A common task when working with modules is importing variables (like configuration settings, constants, or shared data) from one file to another.
🌐
Linus Tech Tips
linustechtips.com › software › programming
Importing variable from another file python - Programming - Linus Tech Tips
August 11, 2020 - I wanna import a variable from another .py document on a python program. Should I import it at the top of the script or is it okay if I import it wherever I want?
🌐
Fabienmaussion
fabienmaussion.info › scientific_programming › week_03 › 01-Import-Scopes.html
Modules, import mechanism, namespaces, scope(s) — Scientific Programming
This can become even more complicated when new modules with their own new variables and functions are used as well. Fortunately, the global scope is constrained to the current script or module, and using external modules is unlikely to cause confusion thanks to the import mechanism: pi = 3.14 import math print(math) print(pi, math.pi) <module 'math' from '/Users/uu23343/.mambaforge/envs/oggm_env/lib/python3.12/lib-dynload/math.cpython-312-darwin.so'> 3.14 3.141592653589793
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch15s04.html
Importing from a Module Whose Name Is Determined at Runtime - Python Cookbook [Book]
July 19, 2002 - You need to import a name from a module, such as from module import name, but module and name are runtime-computed expressions.
Authors: Alex MartelliDavid Ascher
Published: 2002
Pages: 608
🌐
CodeRivers
coderivers.org › blog › python-import-variable-defined-in-another-file
Python: Importing Variables Defined in Another File - CodeRivers
February 22, 2026 - The basic idea is that Python looks for the module in a set of directories specified in the sys.path list. When we import a module, Python creates a module object in memory and executes the code in that module file. Any variables defined in the module become attributes of the module object.