0

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
asitm9
  • 705
  • 4
  • 10
  • 21

1 Answers1

3

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.

Community
  • 1
  • 1
aIKid
  • 21,361
  • 4
  • 36
  • 58
  • when both the file in the same directory then it is fine. but if i would like to import from other directory then what should i do?? and why this approach is not working?? – asitm9 Jun 21 '14 at 06:11
  • if would like treat as a package ?? – asitm9 Jun 21 '14 at 06:18
  • That means, you want to import it from other directories. – aIKid Jun 21 '14 at 06:24
  • yes. but i don't want to set in the PYTHONPATH, better i would like to treat as a package so i can run the same thing in other machine as well. – asitm9 Jun 21 '14 at 06:27
  • Then include the folder to the directory of anything you're working on. There's a reason why modules need to be installed, not just copied.. They set their files and path. That's how you get a package. – aIKid Jun 21 '14 at 06:36
  • If this answers your question, please accept it by ticking the accept button next to this answer! Thank you. – aIKid Jun 21 '14 at 07:28