简体   繁体   中英

Unable to import files in python

I have written Python code which is divided among couple of files ie

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']

(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 (eg, 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()) 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, eg 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") import os ; os.chdir("something") before the other imports is an option, too.
  • set the PYTHONPATH variable to that directory (eg, if your shell is sh or bash: PYTHONPATH=/usr/mario/python/sampletest python main.py
  • modify sys.path[] to include your directory, eg, sys.path.insert(0,"your-modules-path"), as suggested in another answer here.

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')

在每个逗号(例如,导入文件1,文件2,文件3)之后放置空格

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM