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.
How to import variable from another file if another file write the code inside def main():
Importing a variable from another python file imports everything?
How would I import variables from one python file to another when they are in two different folders but in the same directory?
Importing variables from another file in Python - Stack Overflow
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?
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?
The set up is like this:
Dir:
| Folder 1:
| | FileDestination
| Folder 2:
| | SubFolder:
| | | FileWithVariable
Ive tried to do:
import Folder 2.SubFolder.FileWithVariable as Var
This doesn't work and Im not sure why.
Side note, how would I go about typing a folder with a space in its name like 'Folder 2'?
Thanks in advance, I've been thrown into coding in python for research and some things have been a struggle!
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!