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

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
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
Importing variables from another file in Python - Stack Overflow
I have declared a few variables and initialised them with some value in variables.py: flag = 0 j = 1 I want to use those values in another file main_file.py: import variables if(flag == 0) : j ... More on stackoverflow.com
🌐 stackoverflow.com
August 16, 2017
🌐
Medium
medium.com › @gl7526 › importing-variables-from-another-file-5afcdaa6c8df
Importing Variables From Another File | by George | Medium
November 17, 2020 - If we want to choose specific variables to import, we can do so by indicating the variable names (that are in the file we want to import from): One thing to note is that if you have a defined variable with a name that’s the same as the variable you want to import, it will be overwritten after you import the variable. Like usual, Python will run from top to bottom, so the value that the variable takes depends on the order at which you define the variable and import a variable with the same name.
🌐
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 - └> python B.py the value of __name__ in A is A Imported value of a is 0 value of __name__ in B is __main__ The takeaway is that when A is called from B, the whole script of A is executed and the value of __name__ is 'A’ inside the A script. Because __name__ is 'A' , the last print statement in A is not executed. Since we ran the B.py file as a script, the value of __name__ is '__main__'
🌐
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 - To import a variable in Python, you need to use the import statement. 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 ...
🌐
Sololearn
sololearn.com › en › Discuss › 2988478 › python-importing-a-variable-from-another-file-without-its-functions
https://www.sololearn.com/en/Discuss/2988478/pytho...
sl_scroll_/de/Discuss/2689918/i-made-a-bmi-calculator-in-python-and-youa-are-supposed-to-write-the-height-in-meter-i-want-whenever-you-write-the-height-in-cePending
Find elsewhere
🌐
YouTube
youtube.com › watch
PYTHON : Importing variables from another file? - YouTube
PYTHON : Importing variables from another file? [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYTHON : Importing variables fr...
Published: December 5, 2021
🌐
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?

🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
[SOLVED] use function variable from another file - Raspberry Pi Forums
April 10, 2021 - Fri Apr 09, 2021 10:42 pm if you put that into a file called mymodule.py then import it into another python script you access a as mymodule.a A more explict but trivial example: file1.py: ... import file1 a = 101 print(a) print(file1.a) Or, if you want to avoid the "moduelname." prefix, file3.py: ... from file1 import * print(a) a = 202 print(a) What you can't do is modulename.function().variablename or function().variablename Variabes defiined in a fucntion only exist within that function.
🌐
CodeRivers
coderivers.org › blog › python-import-variable-defined-in-another-file
Python: Importing Variables Defined in Another File - CodeRivers
February 22, 2026 - 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. The simplest way to import variables from another file is to import the entire module.
🌐
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...
🌐
Towards Data Science
towardsdatascience.com › home › data science › common mistakes when dealing with multiple python files
Common Mistakes When Dealing with Multiple Python Files | Towards Data Science
May 10, 2021 - In Python, each Python file actually has a specific term called "Module", and when you use the famous import syntax, you are building the connection among several different Python modules to achieve a complex task in your context. Here, I am going to illustrate the following topics: ... If they are something you want to know a bit more about, keep reading! Learn this step by step with the interactive AI and Data Scientist and Computer Science roadmaps. When you create a variable (not within a function) in a Python file, you mount this variable to the namespace of the current module.
🌐
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?
🌐
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
Mutable variables (lists, dicts) modified in the importing file will change the original module’s data. Immutable variables (ints, strs) require importing the entire module to update the original value. Avoid from module import * and circular imports to keep your code maintainable. With these concepts, you’ll confidently manage variables across modules and build cleaner, more organized Python projects.
🌐
pythontutorials
pythontutorials.net › blog › importing-variables-from-another-file
How to Import Variables from Another File: Step-by-Step Guide for Specific and All Variables — pythontutorials.net
from variables import ... tells Python to load the variables module and extract the specified variables. Only the imported variables (PI, API_BASE_URL, user_scores) are available in main.py—others (like MAX_USERS) remain unused, keeping the ...
🌐
YouTube
youtube.com › watch
Importing variables from another file? - YouTube
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn--Music by Eric Matyashttps://www.soundimage.orgTrack title: Lost Civ...
Published: March 12, 2023