Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top