python local modules

痴心易碎 提交于 2019-12-03 15:08:27

Don't mess around with execfile or sys.path.append unless there is some very good reason for it. Rather, just arrange your code into proper python packages and do your importing as you would any other library.

If your mymodules is in fact a part of one large project, then set your package up like so:

myproject/
    __init__.py
    mymodules/
        __init__.py
        myfunctions.py
    myreports/
        __init__.py
        myreportscode.py

And then you can import mymodules from anywhere in your code like this:

from myproject.mymodules import myfunctions
myfunctions.add(1, 2)

If your mymodules code is used by a number of separate and distinct projects, then just make it into a package in its own right and install it into whatever environment it needs to be used in.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!