How to import python file located in same subdirectory in a pycharm project

前端 未结 8 1614
盖世英雄少女心
盖世英雄少女心 2020-12-13 08:48

I have an input error in pycharm when debugging and running.

My project structure is rooted properly, etc./HW3/. so that HW3 is the root di

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 09:06

    If you don't have an __init__.py create one and add this line

    from util.util import my_function
    

    then you can easily import the module in your scripts the __init__.py tells python that it should treat that folder as a python package, it can also be used to import/load modules too.

    in most cases the __init__.py is empty.

    Quoting the docs:

    The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

提交回复
热议问题