What Does a . in an Import Statement in Python Mean

What does a . in an import statement in Python mean?

That's the syntax for explicit relative imports. It means import from the current package.

What does a dot denote in an import statement?

That's the new syntax for explicit relative imports. It means import from the current package.

Without the ., if you had a file _object.py for some indecipherable reason next to your main script, object would break. With the ., it ensures it gets its own module.

The thing that defines what a "current package" is that it should say from where the importing package is. It basically means the current namespace or package directory.

Hope this helps!

Python from [dot]package import ... syntax

The . is a shortcut that tells it to search in the current package before the rest of the PYTHONPATH. So, if a same-named module Recipe exists somewhere else in your PYTHONPATH, it won't be loaded.

Importing in Python what does the . mean?

It is a so called relative import see here and refers to modules or packages located in the same directory.
(More precisely, in the same package, which in most but not all cases will be the same thing.)

What exactly does import * import?

The "advantage" of from xyz import * as opposed to other forms of import is that it imports everything (well, almost... [see (a) below] everything) from the designated module under the current module. This allows using the various objects (variables, classes, methods...) from the imported module without prefixing them with the module's name. For example

>>> from math import *
>>>pi
3.141592653589793
>>>sin(pi/2)
>>>1.0

This practice (of importing * into the current namespace) is however discouraged because it

  • provides the opportunity for namespace collisions (say if you had a variable name pi prior to the import)
  • may be inefficient, if the number of objects imported is big
  • doesn't explicitly document the origin of the variable/method/class (it is nice to have this "self documentation" of the program for future visit into the code)

Typically we therefore limit this import * practice to ad-hoc tests and the like. As pointed out by @Denilson-Sá-Maia, some libraries such as (e.g. pygame) have a sub-module where all the most commonly used constants and functions are defined and such sub-modules are effectively designed to be imported with import *. Other than with these special sub-modules, it is otherwise preferable to ...:

explicitly import a few objects only

>>>from math import pi
>>>pi
>>>3.141592653589793
>>> sin(pi/2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sin' is not defined

or import the module under its own namespace (or an alias thereof, in particular if this is a long name, and the program references its objects many times)

  >>>import math
>>>math.pi
>>>3.141592653589793
etc..

>>>import math as m #bad example math being so short and standard...
>>>m.pi
>>>3.141592653589793
etc..

See the Python documentation on this topic

(a) Specifically, what gets imported with from xyz import * ?

if xyz module defines an __all__ variable, it will import all the names defined in this sequence, otherwise it will import all names, except these which start with an underscore.

Note Many libraries have sub-modules. For example the standard library urllib includes sub-modules like urllib.request, urllib.errors, urllib.response etc. A common point of confusion is that

from urllib import *

would import all these sub-modules. That is NOT the case: one needs to explicitly imports these separately with, say, from urllib.request import * etc. This incidentally is not specific to import *, plain import will not import sub-modules either (but of course, the * which is often a shorthand for "everything" may mislead people in thinking that all sub-modules and everything else would be imported).

why does importing a module executes all statements in python?

That is just how imports work.

def my_function():
print("Hello")

What is the above snippet of code? It is a function definition for sure, but function definitions in Python are statements, and they must be executed in order to define the function. So when you import the above module, it executes the def, which creates a new function and assigns it to my_function. It's basically the same as:

my_function = ...definition..

So when you import a module, you always execute its contents. Otherwise you wouldn't be able to use any functions (or classes) in that module.

There are other ways to define functions in Python for sure.

def create_my_function(x):
def local_function():
print(x)
global my_function
my_function = local_function

create_my_function("Hello")

This is broadly equivalent to the original definition of my_function().

Since you can put any statements in a Python module, Python has no way of knowing which statements must be executed in order to define the symbols you are interested in. So it must execute all the statements.

Python class definition--import statement

You should look up how import and PYTHONPATH work. In your case, you can solve that using:

from Person import Person

I see you're coming from a Java background (where each file must have a class with the same name of the file), but that's not how Python modules work.

In short, when you run a Python script from the command line, as you did, it looks for modules (among other places) in your current dir. When you import a (simple) name like you did, Python will look for:

  1. A file named Basic.py; or:
  2. A folder named Basic with a file named __init__.py.

Then it will look for a definition inside that module named Person.



Related Topics



Leave a reply



Submit