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.
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!
Videos
Note: This answer was intended for a very specific question. For most programmers coming here from a search engine, this is not the answer you are looking for. Typically you would structure your files into packages (see other answers) instead of modifying the search path.
By default, you can't. When importing a file, Python only searches the directory that the entry-point script is running from and sys.path which includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).
However, you can add to the Python path at runtime:
# some_file.py
import sys
# caution: path[0] is reserved for script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
There is nothing wrong with:
from application.app.folder.file import func_name
Just make sure folder also contains an __init__.py. This allows it to be included as a package. I am not sure why the other answers talk about PYTHONPATH.
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?
You can import the variables from the file:
vardata.py
verb_list = [x, y, z]
other_list = [1, 2, 3]
something_else = False
mainfile.py
from vardata import verb_list, other_list
import random
print random.choice(verb_list)
you can also do:
from vardata import *
to import everything from that file. Be careful with this though. You don't want to have name collisions.
Alternatively, you can just import the file and access the variables though its namespace:
import vardata
print vardata.something_else
It’s called importing.
If this is data.py:
verb_list = [
'run',
'walk',
'skip',
]
and this is foo.py:
#!/usr/bin/env python2.7
import data
print data.verb_list
Then running foo.py will access verb_list from data.py.
You might want to work through the Modules section of the Python tutorial.
If verb_list is stored in a script that you want to do other things too, then you will run into a problem where the script runs when all you’d like to do is import its variables. In that case the standard thing to do is to keep all of the script functionality in a function called main(), and then use a magic incantation:
verb_list = [
'run',
'walk',
'skip',
]
def main():
print 'The verbs are', verb_list
if __name__ == '__main__':
main()
Now the code in main() won’t run if all you do is import data. If you’re interested, Python creator Guido van Rossum has written an article on writing more elaborate Python main() functions.