asitm9
asitm9

Reputation: 953

ImportError: No module named

I am a newbie.

Problem statement :

In directory sfdc_bulk i have 2 file 1)helper.py 2)sfdclogin.py

helper.py

import xml.dom.minidom as DOM


def getElemVal(xmlString,elemName):
    #tree = ET.parse('test.xml')
    #print tree
    dom = DOM.parseString(xmlString)
    val=dom.getElementsByTagName(elemName)
    ret=None
    if len(val) >0 :
        ret=val[0].toxml()
        #.replace('<' + ret + '>', '').replace('</' + ret + '>', '')
        ret=ret.replace('<' +elemName+ '>','').replace('</' + elemName + '>', '')
    return ret

sfdclogin.py

from helper import getElemVal

print getElemVal('<?xml version="1.0" encoding="UTF-8"?><foo>bar</foo>', 'foo')

inside the directory sfdc_bulk using ubuntu terminal:

python sfdclogin.py

it returns bar

but after modifying the sfdclogin file to

from sfdc_bulk.helper import getElemVal

print getElemVal('<?xml version="1.0" encoding="UTF-8"?><foo>bar</foo>', 'foo')

i am getting the follwing error:

Traceback (most recent call last):
  File "sfdclogin.py", line 2, in <module>
    from sfdc_bulk.helper import getElemVal
ImportError: No module named sfdc_bulk.helper

Upvotes: 0

Views: 11193

Answers (1)

aIKid
aIKid

Reputation: 28242

If both files are in the same directory, import it directly. Your first try:

from helper import getElemVal

Is already correct. Why change it?

Unless you want to treat sfdc_bulk as a package. Include it in the PYTHONPATH. In Windows it would be like:

$ set PYTHONPATH=%PYTHONPATH%;C:\your\directory\sfdc_bulk

For use in Ubuntu, check out this answer.

Upvotes: 3

Related Questions