Import python package from local directory into interpreter

前端 未结 9 1839
野的像风
野的像风 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:52

    You can use relative imports only from in a module that was in turn imported as part of a package -- your script or interactive interpreter wasn't, so of course from . import (which means "import from the same package I got imported from") doesn't work. import mypackage will be fine once you ensure the parent directory of mypackage is in sys.path (how you managed to get your current directory away from sys.path I don't know -- do you have something strange in site.py, or...?)

    To get your current directory back into sys.path there is in fact no better way than putting it there.

    0 讨论(0)
  • 2020-11-29 03:56

    A simple way to make it work is to run your script from the parent directory using python's -m flag, e.g. python -m packagename.scriptname. Obviously in this situation you need an __init__.py file to turn your directory into a package.

    0 讨论(0)
  • 2020-11-29 04:00

    Inside a package if there is setup.py, then better to install it

    pip install -e .
    
    0 讨论(0)
提交回复
热议问题