from file1 import *
will import all objects and methods in file1
Answer from ennuikiller on Stack Overflowfrom file1 import *
will import all objects and methods in file1
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.
Importing variables from another file in Python - Stack Overflow
How to import variable from another file if another file write the code inside def main():
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.
Stuff undefined importing functions from other file
Everything you've done is right except when using the variables.
In your main_file.py file:
if(variables.flag == 0) :
variables.j = variables.j + 1
(Or)
Use the following header :
from variables import *
(Or)
from variables import flag, j
Replace all the references of flag and j (or any other variable you want to use from that file) with the prefix 'variables.'
Since this is just a copy of the variable, the values in the variables.py won't get affected if you modify them in main_file.py
You can either use
import variables
and then access the vairables like this:
variables.flag
variables.j
or you can use:
from variables import flag, j
and then access the vaiables by just their name.
Important:
Please note that in the second case, you will be working with a copy of the variables, and modifying them in one module has no effect on the variables in the other module!
For example, I want to import all variables from file work.py to use in my current file. The code in work.py is written inside def main():
If I use-> from work import *
It seems to import only variables outside function “main”. I cannot access to the variables created inside function “main”. How to access to variables inside function main?
in model_inspect.py I define
global totalUp
it outside all of the functions. Then, in inference.py I try to import it
from model_inspect import totalUp
Both folders are on the same directory. But I see this error:
ImportError: cannot import name 'totalUp'
Works for me. Here's how:
lib.py:
variable = 42
main.py:
import lib
print lib.variable
Another version of main.py:
from lib import variable
print variable
You don't show your code, so any other hints would be a guess. One guess might be that you have another module called lib.py that is being loaded instead.
Note that import names are case-sensitive, even on Windows.
This is a blind bet (post some code!), but if you imported lib.py, try importing just lib (without .py).
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:
-
https://pastebin.com/zqpp3Auv
-
https://pastebin.com/9PxNzBM7
-
https://pastebin.com/rjeapb8J
Here is the resulting output of the third python file: https://pastebin.com/zT6Nvu5V
What am I doing wrong?