简体   繁体   中英

Python package/module import functions from sub packages

I'm building a python package to do some auditing of systems at work. Thus far I've been able to work with using free floating .py files and and running the main file in the same directory as the module files. At a high level I want to create a package that holds several subpackages for specific audits. bigpackage>subpackage>modulefile.py I want to be able to import specific functions/methods from the modulefile. How can I accomplish this?
I have the __init__ files in place for all my packages and can access them with the usual from bigpackage import modulefile and run them using the dot operator like modulefile.function1(bla, bla, bla) I would like to be able to import specific functions directly so I can run without using the dot operator like function1(bla, bla, bla) .

from bigpackage.subpackage.modulefile import function1, function2

function1(bla, bla, bla)

From: How to import a module given the full path?

my directory layout:

/my_package
    /__init__.py
    /testfile.py
    /my_module
        /__init__.py

method 1:

import imp
my_module = imp.load_sourc('my_module','.my_module/__init__.py')
my_module.function1()

or for restricted importing:

import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from my_module import function1
function1()

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