yrk
yrk

Reputation: 165

Python Imports do not work

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

Upvotes: 2

Views: 4876

Answers (8)

geekofalltrades
geekofalltrades

Reputation: 324

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.

Upvotes: 3

t.pimentel
t.pimentel

Reputation: 1575

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

Upvotes: 1

SVatasoiu
SVatasoiu

Reputation: 171

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.

Upvotes: 1

mstuebner
mstuebner

Reputation: 434

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?

Upvotes: 1

Varun
Varun

Reputation: 107

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.

Upvotes: 1

James Paul Shaulis
James Paul Shaulis

Reputation: 111

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.

Upvotes: 1

shaktimaan
shaktimaan

Reputation: 12092

Having this code in your importtest.py should work:

from thepackage.thesubpackage import mary 
print mary.marie

Upvotes: 3

NPE
NPE

Reputation: 500347

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.

Upvotes: 1

Related Questions