Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from . import * from module

Tags:

python

There is a script in the working directory which I can access with:

from . import core.py

I would also like to import * from core.py. How would I write this in Python?

like image 728
Jacob Valenta Avatar asked Sep 16 '12 21:09

Jacob Valenta


People also ask

What is from import * in Python?

The difference between import and from import in Python is: import imports the whole library. from import imports a specific member or members of the library.

Should we import * in Python?

Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.

What does from file import * do?

It just means that you import all(methods, variables,...) in a way so you don't need to prefix them when using them.

How do I import a function from a module?

Importing Modules To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.


2 Answers

see https://docs.python.org/2/tutorial/modules.html

In section 6.4.2. Intra-package References:

  • If the import module in the same dir, use e.g: from . import core
  • If the import module in the top dir, use e.g: from .. import core
  • If the import module in the other subdir, use e.g: from ..other import core

Note: Starting with Python 2.5, in addition to the implicit relative imports, you can write explicit relative imports with the from module import name form of import statement. These explicit relative imports use leading dots to indicate the current and parent packages involved in the relative import. From the surround module.

like image 157
3 revs, 3 users 73% Avatar answered Oct 22 '22 02:10

3 revs, 3 users 73%


To keep the exact same semantics as from . import core, you'll want to do:

from .core import *
like image 24
Claudiu Avatar answered Oct 22 '22 02:10

Claudiu