1

I'm a Java developer trying to get a hold of Pythog. I'm working on a project which consists out of three components. A server, a client and a common part. (using eclipse)

In the common package I created a module which looks like this:

'''
Created on 4 Jan 2014

@author: christian
'''
import logging

def logDebug(msg):
    logging.log(logging.DEBUG, msg)

def logInfo(msg):
    logging.log(logging.INFO, msg)

def logWarning(msg):
    logging.log(logging.WARNING, msg)

def logError(msg):
    logging.log(logging.ERROR, msg)

def logCritical(msg):
    logging.log(logging.CRITICAL, msg)

It's fairly simple and is just a test. I setup an setyp.py for this project and created an egg-File out of it. This egg-Fille I referenced in the client project. Looking like this:

import logging
from de.christianae.main.common import mylogging

def startUp():
    logging.basicConfig(filename="client.log", level=logging.DEBUG)

    mylogging.logCritical("Test Critical")


if __name__ == '__main__':
    startUp()

What I want to do here is to setup the python loggin mechanism but than use my own module to avoid always having to the the log level.

When I try to run that code I get the following exception:

pydev debugger: starting
Traceback (most recent call last):
  File "/opt/eclipse/plugins/org.python.pydev_3.2.0.201312292215/pysrc/pydevd.py", line                      1706, in <module>
    debugger.run(setup['file'], None, None)
  File "/opt/eclipse/plugins/org.python.pydev_3.2.0.201312292215/pysrc/pydevd.py", line     1324, in run
    pydev_imports.execfile(file, globals, locals)  #execute the script
  File     "/home/christian/repository/synctoolclient/trunk/src/de/christianae/main/client/SyncToolClie    nt.py", line 7, in <module>
    from de.christianae.main.common import mylogging
ImportError: No module named common

What am I doing wrong? Is there a better way to accomplish something like the .jars do in Java?

ThinkChaos
  • 1,803
  • 2
  • 13
  • 21
Chris
  • 515
  • 2
  • 6
  • 13
  • Are you missing the `__init__.py` file? Are `common` and `mylogging` the actual directory and file names? – ThinkChaos Jan 05 '14 at 10:44
  • I have four files in '/de/christianae/main/common/'. '__init__.py', '__init__.pyc', 'mylogging.py' and 'mylogging.pyc'. 'common' is the directory and 'mylogging.py' the filename. – Chris Jan 05 '14 at 10:52
  • I suspect you're not be referencing the egg file when you `import mylogging`, but the directory structure. Try moving the test and egg to a completely different dir tree and see if the error changes. To import from an egg you have to add it to your path : [like this](http://stackoverflow.com/a/16114586). – ThinkChaos Jan 05 '14 at 11:03
  • Are you familiar with eclipse? I add the egg to the Pythonpath there. Is there a file I can check if the path is correct? EDIT: If I move it without changing anything I get an 'Unresolved import: mylogging' from eclipse. When I change the PATH then I get the same exception. – Chris Jan 05 '14 at 11:59
  • I tried using: import sys sys.path.insert(0, '/home/christian/repository/synctoolclient/trunk/lib/de/christianae/main/common/') Same exception – Chris Jan 05 '14 at 12:08
  • Not really. If you added the egg to the python path you won't need the `sys.path` trick, just remember if you take it to a different machine/environment it will fail to find the library as the egg isn't in the path anymore. Make sure the egg's path ends with the `.egg` file. And when you import it, don't do the path like in Java, but the path to it from (PYTHON)PATH's closest dir, here it'll be the egg. It's not clear what the egg actually contains, but if your egg only contains the `common` dir, do `from common import mylogging`. I'm sorry if this isn't really clear, feel free to ask anything – ThinkChaos Jan 05 '14 at 13:11
  • The egg contains the following folders: `/de/christianae/main/common/`. In the common folder is the `mylogging.py` File. The import looks like this: `from de.christianae.main.common import mylogging`. The root folder of the client project is `trunk`. Regarding to eclipse I have two entries in the PYTHONPATH. The fist ist: `/${PROJECT_DIR_NAME}/src` and the secon is `/SyncToolClient/lib/synctoolcommon-0.1-py2.7.egg` – Chris Jan 05 '14 at 13:22

1 Answers1

1

The issue was that his egg's directory was something like this :

| de/
  | __init__.py
  | christianae/
    | __init__.py
    | main/
      | __init__.py
      | common/
        | __init__.py
        | code.py

When he used setup tools to make an egg out of this, it only included the __init__.py from c/, so the import wasn't working as Python didn't see de as a module, and wouldn't go deeper in the tree.

Workaround: flattening to source to common/*.py. Because he's coming from a Java background it was natural for him to have all those directories, but apparently setuptools and distutils don't like that.

ThinkChaos
  • 1,803
  • 2
  • 13
  • 21