-1

Quick question, what is the difference between

import file
import .file

Can someone explain the difference between them? Can someone explain all kinds of importing to me?

DuckyQuack
  • 39
  • 10

2 Answers2

0
import .file

It's the new syntax for explicit relative imports. It means import from the current package. This is the current namespace or package directory.

You can use more than one dot, referring not to the current package but its parent(s). This should only be used within packages, in the main module.

Suraj Kothari
  • 1,642
  • 13
  • 22
  • No, it isn't as it used in the cases I mentioned above. See https://stackoverflow.com/questions/7279810/what-does-a-in-an-import-statement-in-python-mean – Suraj Kothari Oct 17 '18 at 16:48
  • If you look closely, you'll find that not a single post on that page tries to use `import .file` syntax. It's all `from` imports. – user2357112 Oct 17 '18 at 16:49
0

There are two ways to use functions of other modules.

First is importing the whole Module.

import math      #this will import math module
print(math.sqrt(4))  #Using function of math module

Second Method is by importing the function not module

from math import sqrt     #this will import sqrt function not full module
print(sqrt(4))     #Using function of math module

Note some more suggestions 1. you can also import module and function by using as and import it under n alias name, Example import math as ilovemaths #importing under alias name print(ilovemaths.sqrt(4)) #using the function of module.

  1. You can also * to import functions but then the problem will be of conflicting names. So be specific and choose the best method to import.
realmanusharma
  • 330
  • 2
  • 10