question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cant access top level module from sub module

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import Response, status, HTTPException, Depends, APIRouter
from sqlalchemy.orm.session import Session

from app import models, schemas
from app.database import  get_db

Description

I cant access top level module from submodule

this is my structure

├─ app
├─main.py
├─ models.py
├─ schemas.py
|───router
│    ├─── __init__.py
│    ├─── post.py
│    └─── user.py

I want to access models and schemas for my post and user in router but I cant import all module from app I using relative and absolute import and yet python cant find all that

I believe import using the correct way as I following tutorial on official doc and reproduce the same structure from this https://github.com/tiangolo/full-stack-fastapi-postgresql

Operating System

Windows

Operating System Details

Windows 11

FastAPI Version

0.70.1

Python Version

Python 3.10.0

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
tonyhart7commented, Dec 19, 2021

Could be even in the init file. However, I’d say it’s best practice to have it in the scripts where you need them.

Try if it works in any file from your router dir.

Yeah its works, iam using this

import sys
import os
sys.path.append(os.path.dirname(os.getcwd()))

Iam tried to pip install -e . and make my project to the ediable state but I cant find the import

apreciate it

0reactions
DavidSantacruzRcommented, Dec 19, 2021

Something that you can also try is setting the path as an environment variable and accessing it using the OS library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't access top level python package from sub packages
My recommendation would be to change to the directory where the top-level modules and packages are (I guess cd path/to/chatbot/.. in your ...
Read more >
importlib — The implementation of import — Python 3.11.1 ...
An abstract method for finding a spec for the specified module. If this is a top-level import, path will be None . Otherwise,...
Read more >
Modules · The Julia Language
Main is the top-level module and the current module, when Julia is started. Standard library modules. By default Julia ships with some standard...
Read more >
Python File Importation into Multi-Level Directory Modules and ...
As a Python programmer, it is a best practice to abstract application code into different files and directories and according to their related...
Read more >
git-submodule Documentation - Git
Evaluates an arbitrary shell command in each checked out submodule. The command has access to the variables $name, $sm_path, $displaypath, $sha1 and $toplevel: ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found