0

I though it was easy, but I just can't do it. I read in the doc that this should work :

File a/__init__.py

 #nothing here

File a/b.py

var = "hello world"

File a/c.py

import b
print(b.var)

From . :

>>> import a.c
ImportError: No module named 'b'

But it does not… What have I forgotten ? Is it because I use Python 3 ?

Lithy
  • 817
  • 12
  • 23

2 Answers2

1

yes, it's because of Python3.

In Python 3, implicit relative imports within packages are no longer available

Changes in import statement python3

Community
  • 1
  • 1
monitorius
  • 3,566
  • 1
  • 20
  • 17
  • So what is the common way to do so ? Actually I want a special submodule to load in each other submodules. Maybe there is something more idiomatic for that ? – Lithy Jun 21 '14 at 19:48
  • you should explicitly specifiy that you want relative import - `from . import b` – monitorius Jun 21 '14 at 19:58
0

Never mind that, I believe you should save your file in Program Files (x86)/Lib to create a new module, well that is for Python 2.7.6, but it is very possible it will work too for Python 3.0.

Ravenclaw968
  • 1
  • 1
  • 3
  • Srsly I don't think so. Creating a module is as simple as creating a file (actually it is all the same). Creating a package is a little bit more complex but should be easy too… Except here ! – Lithy Jun 21 '14 at 19:42