Import statement works on PyCharm but not from terminal

后端 未结 3 1167
清歌不尽
清歌不尽 2020-12-03 09:25

Pycharm 2016.2.3, Mac OS X 10.11.1, Python 3.5 (Homebrew);

I have this folder structure

project
  /somepackage
    /subpackage
     __init__.py   
           


        
3条回答
  •  情书的邮戳
    2020-12-03 10:05

    You are running foo.py like a script, but you are really using it like a module. So the proper solution is to run it as a module:

    python3 -m somepackage.foo
    

    For the record, another alternative is to edit your path like:

    export PYTHONPATH=.
    

    (Or you could put the absolute directory in there, and of course you should append any other directories that are already in your PYTHONPATH.) This is closer to what PyCharm does, but is less philosophically correct.

提交回复
热议问题