Relative imports in Python 3

前端 未结 16 1076
误落风尘
误落风尘 2020-11-21 06:42

I want to import a function from another file in the same directory.

Sometimes it works for me with from .mymodule import myfunction but sometimes I get

相关标签:
16条回答
  • 2020-11-21 07:11

    I was getting this ImportError: attempted relative import with no known parent package

    In my program I was using the file from current path for importing its function.

    from .filename import function
    

    Then I modified the current path (Dot) with package name. Which resolved my issue.

    from package_name.filename import function
    

    I hope the above answer helps you.

    0 讨论(0)
  • 2020-11-21 07:11

    If none of the above worked for you, you can specify the module explicitly.

    Directory:

    ├── Project
    │     ├── Dir
    │     │    ├── __init__.py
    │     │    ├── module.py
    │     │    └── standalone.py
    

    Solution:

    #in standalone.py
    from Project.Dir.module import ...
    

    module - the module to be imported

    0 讨论(0)
  • TLDR; Append Script path to the System Path by adding following in the entry point of your python script.

    import os.path
    import sys
    PACKAGE_PARENT = '..'
    SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
    sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
    

    Thats it now you can run your project in PyCharma as well as from Terminal!!

    0 讨论(0)
  • 2020-11-21 07:12

    Moving the file from which you are importing to an outside directory helps.
    This is extra useful when your main file makes any other files in its own directory.
    Ex:
    Before:
    Project
    |---dir1
    |-------main.py
    |-------module1.py
    After:
    Project
    |---module1.py
    |---dir1
    |-------main.py

    0 讨论(0)
  • 2020-11-21 07:14

    Put this inside your package's __init__.py file:

    # For relative imports to work in Python 3.6
    import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))
    

    Assuming your package is like this:

    ├── project
    │   ├── package
    │   │   ├── __init__.py
    │   │   ├── module1.py
    │   │   └── module2.py
    │   └── setup.py
    

    Now use regular imports in you package, like:

    # in module2.py
    from module1 import class1
    

    This works in both python 2 and 3.

    0 讨论(0)
  • 2020-11-21 07:18

    For PyCharm users:

    I also was getting ImportError: attempted relative import with no known parent package because I was adding the . notation to silence a PyCharm parsing error. PyCharm innaccurately reports not being able to find:

    lib.thing import function

    If you change it to:

    .lib.thing import function

    it silences the error but then you get the aforementioned ImportError: attempted relative import with no known parent package. Just ignore PyCharm's parser. It's wrong and the code runs fine despite what it says.

    0 讨论(0)
提交回复
热议问题