from file1 import *  

will import all objects and methods in file1

Answer from ennuikiller on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 53229068 › error-importing-variable-from-another-file
python - Error importing variable from another file - Stack Overflow
# Ensure correct file is being imported >>> print(file2.__file__) '~/dev/file2.pyc' # Check the variables in the imported module's scope - note 'array' is listed >>> dir(file2) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', ...
Discussions

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
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
I can't import global variable from another python file

global totalUp just means that the name lookup is global. You still need to make that variable. Usually you would see

global totalUp
totalUp = None # set to initial value

Note that the global keyword is redundant if you are not in a function.

More on reddit.com
🌐 r/learnpython
4
1
May 1, 2020
Stuff undefined importing functions from other file
I’m writing a little software for managing infos for where I work (nothing about IT, coding or stuff like that…). As a total newbie doing by-my-self, I’m writing it in a single file, but something doesn’t work properly and my IDE doesn’t raise any error. More on discuss.python.org
🌐 discuss.python.org
19
0
February 10, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-import-variables-from-another-file-in-python
How to import variables from another file in Python? - GeeksforGeeks
April 3, 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 ...
🌐
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 - As a “former” — C programmer, I am often shocked by some of the things that Python can do. In this post, I will discuss one of these things. Let’s consider two files A.py and B.py in the same folder. ... When the line from A import a is executed, the python interpreter executes the referenced script (A.py) automatically.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
[SOLVED] use function variable from another file - Raspberry Pi Forums
April 10, 2021 - Variables defined in a file but ... python script how you access them depends on how you imported them. You need to do some reading on the scope of a variable in python, its namespaces, and their hierarchy....
Find elsewhere
🌐
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.
🌐
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...
🌐
Python.org
discuss.python.org › python help
Stuff undefined importing functions from other file - Python Help - Discussions on Python.org
February 10, 2023 - I’m writing a little software for managing infos for where I work (nothing about IT, coding or stuff like that…). As a total newbie doing by-my-self, I’m writing it in a single file, but something doesn’t work properly and my IDE doesn’t raise any error.
🌐
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.
🌐
Reddit
reddit.com › r/learnpython › [deleted by user]
[deleted by user] : r/learnpython
March 8, 2021 - You need to import the functions in your file. So let's say you have 2 functions in your useful_functions.py file called function_1 and function_2.
🌐
EyeHunts
tutorial.eyehunts.com › home › python import variable from another file | example code
Python import variable from another file | Example code
August 1, 2022 - First import the file from the current program, then you can import the variable or access the variable in Python. There are three approaches to importing variables from another file.
🌐
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?
🌐
Stack Overflow
stackoverflow.com › questions › 50683385 › python-cant-import-a-variable-from-another-file-even-using-common-ways-to-do
Python - Can't import a variable from another file even using common ways to do that - Stack Overflow
June 4, 2018 - I want to import a variable and it says "ImportError: cannot import name cap" and same for a variable called "resultados". ... Copyimport csv import cv2 from analyzer import resultados cap = cv2.VideoCapture('video.mp4') frame_num = cap.get(cv2.CAP_PROP_FRAME_COUNT) def save(): with open("tabelaSTR.csv", "w") as b: writer = csv.writer(b, lineterminator='\n') writer.writerows(zip(resultados)) ... Copyfrom file import cap from file import save while True: cap.set(cv2.CAP_PROP_POS_FRAMES, f) res, frame = cap.read() ...
🌐
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?