3

I have a file, myfile.py, which imports Class1 from file.py and file.py contains imports to different classes in file2.py, file3.py, file4.py.

In my myfile.py, can I access these classes or do I need to again import file2.py, file3.py, etc.?

Does Python automatically add all the imports included in the file I imported, and can I use them automatically?

2
  • What motivated you to ask this question? Commented Mar 17, 2010 at 2:19
  • I have this opensource code, I downloaded. They are doing something like this. I worked with java and I know that this is not possible in java. So confused and asked.
    – Boolean
    Commented Mar 18, 2010 at 2:04

5 Answers 5

11

Best practice is to import every module that defines identifiers you need, and use those identifiers as qualified by the module's name; I recommend using from only when what you're importing is a module from within a package. The question has often been discussed on SO.

Importing a module, say moda, from many modules (say modb, modc, modd, ...) that need one or more of the identifiers moda defines, does not slow you down: moda's bytecode is loaded (and possibly build from its sources, if needed) only once, the first time moda is imported anywhere, then all other imports of the module use a fast path involving a cache (a dict mapping module names to module objects that is accessible as sys.modules in case of need... if you first import sys, of course!-).

1

Python doesn't automatically introduce anything into the namespace of myfile.py, but you can access everything that is in the namespaces of all the other modules.

That is to say, if in file1.py you did from file2 import SomeClass and in myfile.py you did import file1, then you can access it within myfile as file1.SomeClass. If in file1.py you did import file2 and in myfile.py you did import file1, then you can access the class from within myfile as file1.file2.SomeClass. (These aren't generally the best ways to do it, especially not the second example.)

This is easily tested.

2
  • 5
    But please, please, please don't do this. Explicitly import all modules your code will need. Commented Mar 17, 2010 at 1:19
  • Yes and no. Depending on how file1.py is supposed to work, it might be the right place from which to access SomeClass. (The latter example is always bad form.) Commented Mar 17, 2010 at 2:07
0

In the myfile module, you can either do from file import ClassFromFile2 or from file2 import ClassFromFile2 to access ClassFromFile2, assuming that the class is also imported in file.

This technique is often used to simplify the API a bit. For example, a db.py module might import various things from the modules mysqldb, sqlalchemy and some other helpers. Than, everything can be accessed via the db module.

0

If you are using wildcard import, yes, wildcard import actually is the way of creating new aliases in your current namespace for contents of the imported module. If not, you need to use the namespace of the module you have imported as usual.

0
0

If you are specifically importing a class, then the syntax is something like:

from file import Class1

That means no other entities were imported into the namespace.

More generally, if you import using a wildcard

from file import *

then you will import everything not beginning with '_' in that module or package (the __init__.py file in the file package directory). If __all__ is defined in that module or package, then it will only import the symbols from that list.

Lastly, if you do a straight import of a module or package, then all symbols from that module or package are imported:

import file
from package import file
  • Note - file here is the filename minus the `.py` extension - I used that because that was in the OP's example.
  • Note - technically, all defined things are accessible - but python assumes you are accessing things you were meant to access. You would need to chain the namespaces together:
    file.file2.class2
    

    But this is bad since it may violate the "interface" the module or package was trying to define for you.

  • Note - modules define their interface using the `__all__` variable. A package is a directory with an `__init__.py` module in it. But the `__all__` list only works when you use the `from x import *` format.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.