How do I import other files in Python?
- How exactly can I import a specific python file like
import file.py
? - How can I import a folder instead of a specific file?
- I want to load a Python file dynamically at runtime, based on user input.
- I want to know how to load just one specific part from the file.
For example, in main.py
I have:
from extra import *
Although this gives me all the definitions in extra.py
, when maybe all I want is a single definition:
def gap():
print
print
What do I add to the import
statement to just get gap
from extra.py
?
Import doc .. -- Link for reference
The
__init__.py
files are required to make Python treat the directories as containing packages, this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.__init__.py
can just be an empty file, but it can also execute initialization code for the package or set the__all__
variable.and
Here are the two simple ways I have understood by now and make sure your "file.py" file which you want to import as a library is present in your current directory only.
the best way to import .py files is by way of
__init__.py
. the simplest thing to do, is to create an empty file named__init__.py
in the same directory that your.py file is located.this post by Mike Grouchy is a great explanation of
__init__.py
and its use for making, importing, and setting up python packages.How I import is import the file and use shorthand of it's name.
Don't forget that your importing file MUST BE named with .py extension
I'd like to add this note I don't very clearly elsewhere; inside a module/package, when loading from files, the module/package name must be prefixed with the
mymodule
. Imaginemymodule
being layout like this:When loading
somefile.py
/otherstuff.py
from__init__.py
the contents should look like:There are many ways, as listed above, but I find that I just want to import he contents of a file, and don't want to have to write lines and lines and have to import other modules. So, I came up with a way to get the contents of a file, even with the dot syntax (
file.property
) as opposed to merging the imported file with yours.First of all, here is my file which I'll import,
data.py
Note: You could use the
.txt
extension instead.In
mainfile.py
, start by opening and getting the contents.Now you have the contents as a string, but trying to access
data.testString
will cause an error, asdata
is an instance of thestr
class, and even if it does have a propertytestString
it will not do what you expected.Next, create a class. For instance (pun intended),
ImportedFile
And put this into it (with the appropriate indentation):
And finally, re-assign
data
like so:And that's it! Just access like you would for any-other module, typing
print(data.testString)
will print to the consoleA string literal to import and test with
.If, however, you want the equivalent of
from mod import *
just drop the class, instance assignment, and de-dent theexec
.Hope this helps:)
-Benji