简体   繁体   中英

Manually loaded configuration script leads to a weird path behavior

I've created a small command line utility. I would like to let users customize the behavior of the command by setting the path to a python script, however when I load this script using:

import imp
my_script = imp.load_source('script', script_path)

However the import within the script are not working anymore, what was working before now fails with No module named lib

import mycommand.lib as my_lib

Here is the structures of my files:

mycommand/
    __init__.py
    command.py
    lib/
        __init__.py
external_script/
    __init__.py
    script.py

I do my tests typing:

python mycommand/command.py external_script/script.py

I hope I'm clear enough. I'm running Python 2.7.

imp.load_source() first argument is the name you want the script to be imported as, in your case it's being called 'script'. Probably will work if you change 'script' to 'mycommand'?

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