Reputation: 10830
I am currently doing a python tutorial, but they use IDLE, and I opted to use the interpreter on terminal. So I had to find out how to import a module I created. At first I tried
import my_file
then I tried calling the function inside the module by itself, and it failed. I looked around and doing
my_file.function
works. I am very confused why this needs to be done if it was imported. Also, is there a way around it so that I can just call the function? Can anyone point me in the right direction. Thanks in advance.
Upvotes: 4
Views: 1744
Reputation: 2516
If you wanted to use my_file.function
by just calling function
, try using the from
keyword.
Instead of import my_file
try from my_file import *
.
You can also do this to only import parts of a module like so :
from my_file import function1, function2, class1
To avoid clashes in names, you can import things with a different name:
from my_file import function as awesomePythonFunction
EDIT:
Be careful with this, if you import two modules (myfile, myfile2
) that both have the same function
inside, function
will will point to the function
in whatever module you imported last. This could make interesting things happen if you are unaware of it.
Upvotes: 8
Reputation: 184161
A basic import
statement is an assignment of the module object (everything's an object in Python) to the specified name. I mean this literally: you can use an import
anywhere in your program you can assign a value to a variable, because they're the same thing. Behind the scenes, Python is calling a built-in function called __import__()
to do the import, then returning the result and assigning it to the variable name you provided.
import foo
means "import module foo
and assign it the name foo
in my namespace. This is the same as:
foo = __import__("foo")
Similarly, you can do:
import foo as f
which means "import module foo
and assign it the name f
in my namespace." This is the same as:
f = __import__("foo")
Since in this case, you have only a reference to the module object, referring to things contained by the module requires attribute access: foo.bar
etc.
You can also do from foo import bar
. This creates a variable named bar
in your namespace that points to the bar
function in the foo
module. It's syntactic sugar for:
bar = __import__("foo").bar
Upvotes: 5
Reputation: 7955
I'm going to incorporate many of the comments already posted.
To have access to function
without having to refer to the module my_file
, you can do one of the following:
from my_file import function
or
from my_file import *
For a more in-depth description of how modules work, I would refer to the documentation on python modules.
The first is the preferred solution, and the second is not recommended for many reasons:
Python imports work differently than the #includes/imports in a static language like C or Java, in that python executes the statements in a module. Thus if two modules need to import a specific name (or *) out of each other, you can run into circular referencing problems, such as an ImportError
when importing a specific name, or simply not getting the expected names defined (in the case you from ... import *
). When you don't request specific names, you don't run into the, risk of having circular references, as long as the name is defined by the time you actually want to use it.
The from ... import *
also doesn't guarantee you get everything. As stated in the documentation on python modules, a module can defined the __all__
name, and cause from ... import *
statements to miss importing all of the subpackages, except those listed by __all__
.
Upvotes: 0
Reputation: 309899
This is a central concept to python. It uses namespaces (see the last line of import this
). The idea is that with thousands of people writing many different modules, the likelihood of a name collision is reasonably high. For example, I write module foo
which provides function baz
and Joe Smith writes module bar
which provides a function baz
. My baz
is not the same as Joe Smiths, so in order to differentiate the two, we put them in a namespace (foo
and bar
) so mine can be called by foo.baz()
and Joe's can be called by bar.baz()
.
Of course, typing foo.baz()
all the time gets annoying if you just want baz()
and are sure that none of your other modules imported will provide any problems... That is why python provides the from foo import *
syntax, or even from foo import baz
to only import the function/object/constant baz
(as others have already noted).
Note that things can get even more complex: Assume you have a module foo which provides function bar and baz, below are a few ways to import and then call the functions contained inside foo...
import foo # >>> foo.bar();foo.baz()
import foo as bar # >>> bar.bar();bar.baz()
from foo import bar,baz # >>> bar(); baz()
from foo import * # >>> bar(); baz()
from foo import bar as cow # >>> cow() # This calls bar(), baz() is not available
...
Upvotes: 5
Reputation: 599580
I don't really understand your confusion. You've imported the name my_file
, not anything underneath it, so that's how you reference it.
If you want to import functions or classes inside a module directly, you can use:
from my_file import function
Upvotes: 1