Attempted relative imports to non-batch

My project has the following structure:

+main_proj

    +modules
        +user_ops
            -add_user.py
            -remove_user.py
            -__init.py
    +common
        -user_operations.py
        -__init__.py
    -main.py
    -__init__.py

My problem is that add_user.py, and remove_user.pyboth depend on user_operations, and when I try to call them, I get an error relative import in non-package.

Main.py

from common import user_operations as user_operations  #This works ok

if __name__ == '__main__':

    command = "remove_user"

    #Load usage, functions and paths to modules
    __doc__, function_dictionary, modules = utilities.load_modules()

    #Get function from dictionary
    func = function_dictionary[command]

    #Execute function
    result = func(api_root, arguments, simulate, headers)

Module called func(api_root, arguments, simulate, headers)

remove_user_front.py

from ...common import user_operations as user_operations

    def remove_user_front(api_root, arguments, simulate, headers):

        #Get users in this account
        users = user_operations.get_users(api_root, arguments["<source_account>"], headers)

        #This is smelly
        if arguments["--id"]:
            type = "id"
        elif arguments["--username"]:
            type = "username"
        #End smell

        result = remove_user(api_root, arguments["<source_account>"], arguments["<user_id>"], type, simulate, headers)

        if not result:
            return False
        else:
            return True

    function_dictionary = {"remove_user" : remove_user_front}

Error

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_util.py", line 7
6, in exec_file
    exec(code_obj, global_variables)
  File "C:\Users\mryan\Documents\Code\cli\cli_front.py", line
 70, in <module>
    recovery.start_recovery(recovery_file, auto_recovery, headers, modules)
  File "C:\Users\mryan\Documents\Code\cli\recovery.py", line 1
01, in start_recovery
    imported_module = imp.load_source('recovery',module)
  File "C:\Users\mryan\Documents\Code\cli\modules\cam_ops\
remove_user_front.py", line 1, in <module>
    from ...common import user_operations as user_operations
ValueError: Attempted relative import in non-package

Does anyone know how I can save my file structure and include modules under modulesto use modules under common?

+3
source share
1 answer

When the application starts, it receives the os.path attribute.

, -, . , , user_ops, , main.py, .

from modules.user_ops import add_user

user_operations

+2

All Articles