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 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?
python import as a variable name - Stack Overflow
How to import variable from another file if another file write the code inside def main():
Python doesn't have a feature that's directly equivalent to PHP's "variable variables". To get a "variable variable"'s value (or the value of any other expression) you can use the eval function.
foo = "Hello World"
print eval("foo")
However, this can't be used in an import statement.
It is possible to use the __import__ function to import using a variable.
package = "os"
name = "path"
imported = getattr(__import__(package, fromlist=[name]), name)
is equivalent to
from os import path as imported
Old thread, but I needed the answer, so someone else still might...
There's a cleaner way to do this in Python 2.7+:
import importlib
my_module = importlib.import_module("package.path.%s" % module_name)
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!
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.
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.
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?
You want the built in __import__ function
new_module = __import__(modulename)
importlib is probably the way to go. The documentation on it is here. It's generally preferred over __import__ for most uses.
In your case, you would use:
import importlib
module = importlib.import_module(module_name, package=None)
What you want to do is dynamically importing a module starting from a string describing the path of the module. You can do this by using import_module from the importlib package.
import importlib
def importvar (var, fname, notfound) :
try:
return getattr(importlib.import_module(fname), var)
except:
return notfound
This should give you the clue:
>>> from importlib import import_module
>>> config = import_module('config')
>>> print( getattr(config, 'variable') )
value
See the docs for getattr.
Basically, getattr(x, 'variable') is equivalent to x.variable