简体   繁体   中英

Python: import failing in installed python custom package

I have a python package created using setup.py and then installed in a virtual environment. The directory structure is:

root
 |
 +-- setup.py
 |
 +-- package
      |
      +-- __init__.py
      |
      +-- main.py
      |
      +-- common
           |
           +-- __init__.py
           |
           +-- helper.py

I have a method that exist in helper.py and importing in main.py by from common.helper import func . After creating a wheel of this and installing and using that as a global module I am getting an import error for common . But changing the import statement to from package.common.helper import func works. Is there a way to define the common as a local submodule to package so that even if is installed as a package it uses common as a local submodule.

You should use from .common.helper import func . You code is failing because it cannot recognise common as a package globally, since it is subpackage to package directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM