0

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).

SaidbakR
  • 13,303
  • 20
  • 101
  • 195

2 Answers2

2
from bigpackage.subpackage.modulefile import function1, function2

function1(bla, bla, bla)
nivix zixer
  • 1,611
  • 1
  • 13
  • 19
  • right the reason behind this is i want to restrict access to the functions so a clever user wouldnt be able to do something unwanted. edit: nevermind that totally worked! thx – Casey Sutherland May 21 '15 at 17:21
  • If this helped you, please accept the answer. Stack Overflow saves a newborn puppy from a burning building each time an answer is accepted. – nivix zixer May 21 '15 at 17:47
0

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()
Community
  • 1
  • 1
Hoboken515
  • 11
  • 3