Forked from JungeAlexander/import-package-from parent.py
Created
March 4, 2022 02:24
-
-
Save aisuhua/33223dad4eb42e4fbf7b94b6c05aeecb to your computer and use it in GitHub Desktop.
Import modules from parent folder in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # From http://stackoverflow.com/a/11158224 | |
| # Solution A - If the script importing the module is in a package | |
| from .. import mymodule | |
| # Solution B - If the script importing the module is not in a package | |
| import os,sys,inspect | |
| current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) | |
| parent_dir = os.path.dirname(current_dir) | |
| sys.path.insert(0, parent_dir) | |
| import mymodule |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment