2

For finally testing modules and subpackages I made a new folder on my Mac OSX Mavericks. Location of the folder is the Desktop:

packtest
 |-- importtest.py
 |-- thepackage 
      |-- __init__.py
      |-- thesubpackage
           |-- __init__.py
           |-- mary.py

the mary.py contains a variable:

marie="Hello"

and nothing else.

In the importtest.py I want to print it out. I tried different ways of importing, basically the ones stated in this Python: importing a sub‑package or sub‑module thread.

Also I tried to add __all__ = ["mary"] to the __init__.py in the thesubpackage folder.

But all I tried did not work. Any Ideas?


Edit:

When trying the suggested solutions I got these errors:

import thepackage.thesubpackage.mary
print thepackage.thesubpackage.mary.marie

results in:

$ python importtest.py
Traceback (most recent call last):
  File "importtest.py", line 1, in <module>
    import thepackage.thesubpackage.mary
ImportError: No module named thepackage.thesubpackage.mary

When trying:

from thepackage.thesubpackage import mary
print mary.marie

The error is:

$ python importtest.py
Traceback (most recent call last):
  File "importtest.py", line 1, in <module>
    from thepackage.thesubpackage import mary
ImportError: No module named thepackage.thesubpackage
Community
  • 1
  • 1
yrk
  • 155
  • 1
  • 3
  • 17
  • How are you importing it? – kylieCatt Aug 07 '14 at 19:04
  • 1
    "tried all the X on this other page" is not as useful as showing specific code. Often, the mistake is somewhere in the details of _how_ you tried to apply the thing given elsewhere, and thus impossible to diagnose without details. – Charles Duffy Aug 07 '14 at 19:05
  • Added the two versions of NPE to the question. Both error messages are quite common when trying all kinds of versions. – yrk Aug 07 '14 at 20:07
  • Is the base of the package in your `sys.path`? – Keith Aug 09 '14 at 20:03
  • @yrk Can you check if the spelling of directory `thesubpackage` is correct on your system? – Anshul Goyal Aug 10 '14 at 09:35
  • @mu無 yes it is correct. – yrk Aug 10 '14 at 16:03
  • your imports should work. – salmanwahed Aug 14 '14 at 06:57
  • But they don't , that's why I am asking. – yrk Aug 14 '14 at 08:33
  • Did you add an empty `__init__.py` file in `thepackage` directory also? Or only in the `thesubpackage`? It is necessary to have it in both – t.pimentel Aug 15 '14 at 02:49
  • I have one piece of advice on writing questions like this - try to avoid sweeping generalities like "Python imports do not work". Anyone qualified to answer the question obviously knows and trusts that in general, they do work. They may not work in a specific situation - if you're able to reflect that in the title, you will probably get more responses, since it will communicate that you've tried to classify the failure conditions more precisely. – Jordan Samuels Aug 15 '14 at 16:50

8 Answers8

3

Having this code in your importtest.py should work:

from thepackage.thesubpackage import mary 
print mary.marie
shaktimaan
  • 11,962
  • 2
  • 29
  • 33
  • Do you mean in the importtest.py? That gives me Traceback (most recent call last): File "importtest.py", line 1, in from thepackage.thesubpackage import mary ImportError: No module named thepackage.thesubpackage – yrk Aug 07 '14 at 19:18
  • @yrk Yes, My bad. Edited to fix that. Are you sure your `__init__.py` is empty in both `thepackage` and `thesubpackage`? – shaktimaan Aug 07 '14 at 19:23
3

Are 'thepackage', 'thesubpackage', etc. actually the names of the packages you're working with, or have you substituted them here as an example? If the names are different, then you might be suffering from a name collision. Try this, but with the actual name of the package, if it's different:

From within your 'packtest' directory, start a python interpreter, and type:

>>> import thepackage

Did it work? If so, try:

>>> thepackage.__path__

You should see ['thepackage']. If you see something different, that's your problem: you're importing a different package named thepackage, and it probably doesn't have a thesubpackage.mary module, which is where the ImportError is coming from. I'm not sure why this would be the case; Python should be searching in the local directory first, then looking through your PATH and your PYTHONPATH.

EDIT: Here's another possibility: what are the permissions on your 'thepackage' directory and its nested subdirectories? I did an experiment:

$ mkdir something
$ touch something/__init__.py
$ chmod 000 something
$ python
>>> import something
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named something
>>>
$ chmod 744 something
$ python
>>> import something
>>>

To see the permissions, enter ls -l from a terminal within your 'packtest' directory. The first column of output is the permissions.

geekofalltrades
  • 214
  • 1
  • 10
  • I created the whole packtest only for testing purpose so the names were exactly the same. I suffered from the permissions problem. Thank you for the help! – yrk Aug 17 '14 at 16:25
1

If the layout is exactly as shown, and assuming empty __init__.py, any the following should work in importest.py:

import thepackage.thesubpackage.mary
from thepackage.thesubpackage import mary

In the first case you need to reference the string as thepackage.thesubpackage.mary.marie. In the second, mary.marie.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

If mary or marie is a module, then it must be on the Desktop or in Python's directory. Move the missing module to the Desktop folder and then add mary or marie to something like /usr/lib/python* directory.

1

Did you tried imp module? And I guess it would be better to traverse from the header directory i.e the absolute path.

../

import imp foo = imp.load_source('file', 'File\Directory\file.py')

then foo will be the name of the module for example foo.method()

../

And do have a look at these : Import a module from a relative path and How to do relative imports in Python? , I'm sure you will definitely find something here for your case too.

Community
  • 1
  • 1
Varun
  • 107
  • 3
  • 10
1

Even that others confirmed that already I tested your setup, by creating your structure and contents and finally wrote the following in importtest.py

import thepackage.thesubpackage.mary as mary
print(mary.marie)

import thepackage.thesubpackage.mary
print(thepackage.thesubpackage.mary.marie)

Both worked without setting anything in PYTHONPATH or similar, running from a simple shell. As I did so sucessfully on Windows7 (32bit) but it doesn't work on you Mac, shouldn't it something specific for Mac that makes the difference?

mstuebner
  • 406
  • 2
  • 15
1

Try this:

    import sys
    sys.path.append("thepackage/thesubpackage")
    from thepackage.thesubpackage import mary

    print mary.marie

You should add the package to the path in order to import from it.

SVatasoiu
  • 171
  • 4
1

You have to add an __init__.py empty file in both the thepackage and thesubpackage folders, not just in the thesubpackage one.

Then this simple code should work:

from thepackage.thesubpackage import mary
print mary.marie
t.pimentel
  • 1,465
  • 3
  • 17
  • 24