16

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?

alex
  • 6,818
  • 9
  • 52
  • 103
Jacob Valenta
  • 6,659
  • 8
  • 31
  • 42

3 Answers3

15

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.

Leslie Zhu
  • 139
  • 2
  • 7
12

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

from .core import *
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • 3
    What is the advantage to doing this specific syntax, with the dot before the word? Is it to force to import from `core.py` in the current directory (i.e. perhaps there's another `core` module)? I thought python always searched and imported from the current directory first, no? – Nate Feb 28 '16 at 20:44
11

I'm pretty sure it's just:

from core import *

Assuming core.py is in your current working directory or where the script is running from.

mjgpy3
  • 8,597
  • 5
  • 30
  • 51