Import python package from local directory into interpreter

前端 未结 9 1860
野的像风
野的像风 2020-11-29 02:54

I\'m developing/testing a package in my local directory. I want to import it in the interpreter (v2.5), but sys.path does not include the current directory. Right now I type

9条回答
  •  天涯浪人
    2020-11-29 03:50

    See the documentation for sys.path:

    http://docs.python.org/library/sys.html#sys.path

    To quote:

    If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.

    So, there's no need to monkey with sys.path if you're starting the python interpreter from the directory containing your module.

    Also, to import your package, just do:

    import mypackage
    

    Since the directory containing the package is already in sys.path, it should work fine.

提交回复
热议问题