hugapi / hug

Embrace the APIs of the future. Hug aims to make developing APIs as simple as possible, but no simpler.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Splitting APIs over multiple files

sugizo opened this issue · comments

something.py

import hug
@hug.get('/')
def say_hi():
    return 'hello from something'

init.py

import hug
from . import something
@hug.get('/')
def say_hi():
    return "Hi from root"
@hug.extend_api('/something')
def something_api():
    return [something]
$ python __init__.py 
Traceback (most recent call last):
  File "__init__.py", line 2, in <module>
    from . import something
ImportError: attempted relative import with no known parent package
$ hug -f __init__.py 
Traceback (most recent call last):
  File "/Users/MacBookPro/miniconda3/envs/python3_test/bin/hug", line 8, in <module>
    sys.exit(development_runner.hug.interface.cli())
  File "/Users/MacBookPro/miniconda3/envs/python3_test/lib/python3.8/site-packages/hug/interface.py", line 650, in __call__
    raise exception
  File "/Users/MacBookPro/miniconda3/envs/python3_test/lib/python3.8/site-packages/hug/interface.py", line 646, in __call__
    result = self.output(self.interface(**pass_to_function), context)
  File "/Users/MacBookPro/miniconda3/envs/python3_test/lib/python3.8/site-packages/hug/interface.py", line 129, in __call__
    return __hug_internal_self._function(*args, **kwargs)
  File "/Users/MacBookPro/miniconda3/envs/python3_test/lib/python3.8/site-packages/hug/development_runner.py", line 65, in hug
    api_module = importlib.machinery.SourceFileLoader(file.split(".")[0], file).load_module()
  File "<frozen importlib._bootstrap_external>", line 462, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 962, in load_module
  File "<frozen importlib._bootstrap_external>", line 787, in load_module
  File "<frozen importlib._bootstrap>", line 265, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 702, in _load
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "__init__.py", line 2, in <module>
    from . import something
ImportError: attempted relative import with no known parent package

not work on hug-2.6.1
python 3.8

question
how to run hug rest api over multiple files ?

thanks