3

I have written Python code which is divided among couple of files i.e.

record_1.py
record_2.py
record_3.py
record_4.py
main.py

All these files are placed in a common folder:

/user/mario/python/sampletest

When I am trying to import the above files in main.py it's giving error While executing the below command:

$] python  main.py
ImportError: No module named record_1

Below is the main.py code:

import jaydebeapi
import record_1,record_2,record_3,record_4


def main():
def teradata_conn():
    try:
        conn_teradata = jaydebeapi.connect(jclassname='com.teradata.jdbc.TeraDriver',
                                           url="jdbc:teradata://10.10.10.10",
                                           driver_args=['@user','@pass'],
                                           jars=['/user/mario/python/jar/tdgssconfig.jar','/user/mario/python/jar/terajdbc4.jar'])
        print("Connection was successful")            
        record_1()
        record_2()
        record_3()
        record_4()

    except Exception as e:
        print(e)


if __name__ == '__main__':
    main()

Any help or suggestion is highly appreciated.

import sys
print (repr(sys.path))


['', '/user/mario/anaconda2/lib/python27.zip', 
'/user/mario/anaconda2/lib/python2.7', 
'/user/mario/anaconda2/lib/python2.7/plat-darwin',
'/user/mario/anaconda2/lib/python2.7/plat-mac', 
'/user/mario/anaconda2/lib/python2.7/plat-mac/lib-scriptpackages', 
'/user/mario/anaconda2/lib/python2.7/lib-tk', 
'/user/mario/anaconda2/lib/python2.7/lib-old', 
'/user/mario/anaconda2/lib/python2.7/lib-dynload', 
'/user/mario/anaconda2/lib/python2.7/site-packages', 
'/user/mario/anaconda2/lib/python2.7/site-packages/aeosa', 
'/user/mario/anaconda2/lib/python2.7/site
packages/IPython/extensions', 
'/user/mario/.ipython']
user9975773
  • 33
  • 1
  • 5

2 Answers2

4

(info from https://docs.python.org/2/library/sys.html#sys.path):

For import to work, the directory where the modules are must be in the path (sys.path). The path will normally include the directory where the script you're running is located, but this may be broken if Python is unable to determine where that is (e.g., if you do something like python <script.py ). In such cases, Python should add the empty string to the path, meaning 'current directory'.

From the output of print (repr(sys.path)), it appears that the script was ran in a way that prevented Python from knowing where it is located - Python inserted the empty string "" as the first item in the path, this happens when you do something like this:

python  </usr/mario/python/sampletest/main.py

Your system modules paths (.../anaconda2/lib/...) suggests an installation of Python that I am not familiar with. If you actually ran the command exactly as you quoted it (python main.py - when the current directory is where both main and the modules are), it should have worked - but in your installation, the python command might be some wrapper script that changes directories or does other things before running the interpreter that mess it up.

Even though it didn't have "/usr/mario/python/sampletest" in the path, having "" in the path makes Python look for modules in the current directory. Given that in your case you got a failure, it means that (at the time the script started), the current directory was NOT /usr/mario/python/sampletest. See what import os ; print (os.getcwd()) will give you, if it isn't your modules directory, that would explain it.

Likely solutions to fix the problem, choose depending on your needs:

  • give the full path to your script to python, e.g. python /usr/mario/python/sampletest/main.py.
  • try ensuring that the current directory is /usr/mario/python/sampletest when you run the script. Using import os ; os.chdir("something") before the other imports is an option, too.
  • set the PYTHONPATH variable to that directory (e.g., if your shell is sh or bash: PYTHONPATH=/usr/mario/python/sampletest python main.py
  • modify sys.path[] to include your directory, e.g., sys.path.insert(0,"your-modules-path"), as suggested in another answer here.
Leo K
  • 5,189
  • 3
  • 12
  • 27
  • Thank you Leo. I have shared the output with the main question. – user9975773 Jun 22 '18 at 05:10
  • I tried the way you suggested but still not getting the output. Have shared the results in the original question. – user9975773 Jun 22 '18 at 17:26
  • From your latest update, it seems you got the import paths right. Are you getting ImportError or something else? I see a different problem in your code: you have record_1(), and record_1 - if it got imported successfully - is not actually callable (it is a module object, you cannot call that). – Leo K Jun 22 '18 at 18:05
  • Thanks a lot @Leo K. There were certain things which I did in my code along with the suggestions posted here. Now its working. – user9975773 Jun 22 '18 at 18:17
-2

I'm not an expert in Python. I vaguely remember running into this issue and when I added the following, it worked. But again, I was trying to import modules that are in different directory.

sys.path.insert(0,'/user/mario/python/sampletest')
P Raju
  • 329
  • 2
  • 14
  • What's the significance of (0,' ') ? – user9975773 Jun 22 '18 at 05:11
  • Here's the link to a post which explained the significance https://stackoverflow.com/questions/31291608/effect-of-using-sys-path-insert0-path-and-sys-pathappend-when-loading-modul – P Raju Jun 22 '18 at 17:24
  • vaguely remembering things not relevant to the question is not a good basis to answer on SO. – RichieHH Mar 31 '21 at 16:48