简体   繁体   中英

python: import function doesnt work in 3.5

I use this to import a function in 2.7 which works:

def import_x():
    import os
    import imp
    directory = os.path.dirname(os.path.realpath(__file__))
    os.environ['__PYTHON_SYS_PATH_FOR_TESTFOLDER__'] = directory
    fp, pathname, description = imp.find_module('test', [ directory ])
    if fp is not None:
        try:
            x = imp.load_module('test', fp, os.path.realpath(pathname), description)
        finally:
            fp.close()
        return x

test = import_x()

But I struggle to make this work in 3.5. Any idea please?

Try this instead:

import sys
directory = os.path.dirname(os.path.abspath(sys.argv[0]))

I'm not sure about the specific change, but this seems to work.

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