简体   繁体   中英

I'm getting confused with python imports

I have the following project structure:

在此处输入图像描述

within blog_posts.py I have the following function and router:

# blog_post.py

router = APIRouter(
    prefix="/blog",
    tags=["blog"]
)

def required_functionality():
    return {"message": "Learning FastAPI is important"}

within blog_get.py I also have a router and import the required_functionality function:

# blog_get.py

from blog_posts import required_functionality

router = APIRouter(
    prefix="/blog",
    tags=["blog"]
)

a = required_functionality()

Finally inside main.py im importing both routers:

# main.py

from router import blog_get, blog_posts

app = FastAPI()
app.include_router(blog_get.router)
app.include_router(blog_posts.router)

If I then run main.py I get the following error:

Traceback (most recent call last):
  File "C:\Users\UKGC\PycharmProjects\FastAPI\Routers\main.py", line 6, in <module>
    from router import blog_get, blog_posts
  File "C:\Users\UKGC\PycharmProjects\FastAPI\Routers\router\blog_get.py", line 4, in <module>
    from blog_posts import required_functionality
ModuleNotFoundError: No module named 'blog_posts'

If i then change the import in blog_get.py to:

# blog_get.py

from router.blog_posts import required_functionality

router = APIRouter(
    prefix="/blog",
    tags=["blog"]
)

a = required_functionality()

and run main.py again I have no issues, however, if I run blog_get.py directly I get the following error:

Traceback (most recent call last):
  File "C:\Users\UKGC\PycharmProjects\FastAPI\Routers\router\blog_get.py", line 4, in <module>
    from router.blog_posts import required_functionality
ModuleNotFoundError: No module named 'router'

How can I get main.py and blog_get.py to both work? I clearly don't seem to understand python imports properly, can someone explain what I'm missing?

I've tried converting router to a package, I've tried relative imports.

You should use relative imports, not absolute imports

# blog_get.py

from .blog_posts import required_functionality

When you execute main.py , there is no directory on your module search path that contains blog_posts.py . But there is a module router.blog_posts , and blog_get (which tries to import blog_posts ) is in the package router as well.

To be able to run both scripts and get them to work I needed to append the 'router' to sys.path when importing from main.py . This is a bit of a hacky solution, im still open to other solutions?

# main.py
from router import blog_get, blog_posts

app = FastAPI()
app.include_router(blog_get.router)
app.include_router(blog_posts.router)

and

# blog_get.py
import os
import sys
fpath = os.path.dirname(__file__)
sys.path.append(fpath)

from blog_posts import required_functionality

router = APIRouter(
    prefix="/blog",
    tags=["blog"]
)

a = required_functionality()

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