Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow uvicorn.run to also accept app-dir kwarg #1126

Closed
2 tasks done
PietroPasotti opened this issue Jul 16, 2021 · 3 comments · Fixed by #1271
Closed
2 tasks done

allow uvicorn.run to also accept app-dir kwarg #1126

PietroPasotti opened this issue Jul 16, 2021 · 3 comments · Fixed by #1271

Comments

@PietroPasotti
Copy link

Checklist

  • There are no similar issues or pull requests for this yet.
  • I discussed this idea on the community chat and feedback is positive.

Is your feature related to a problem? Please describe.

The issue is: I have a script using uvicorn.run, but I can only run it from a specific cwd, else it will fail with ImportError.

Describe the solution you would like.

I would like the --app-dir functionality to be available in uvicorn.run() as well.
I.e.: to be able to run a script containing a call to uvicorn.run regardless of the cwd.

Describe alternatives you considered

Doing it myself, i.e. calling sys.path.insert(0, abs_path_to_my_project_root_folder), but it feels to me that if this functionality is implemented in main() but not in run() (which from reading the code are the same thing) there must be something wrong with the uvicorn code.

Additional context

#549
#619
77468df

My guess is that this commit implemented the behaviour in the wrong place. Instead of 'consuming' the app_dir argument inside main(), it should have been passed down as-is to run() where it will be processed.

(I volunteer to do the job, if we agree that there is a problem)

@command-tab
Copy link

Yes, please! I just ran into this trying to run uvicorn programmatically, with a simple directory structure like:

manage.py          # contains `uvicorn.run(...)`
app/
    __init__.py    # empty
    main.py        # contains `app = FastAPI(...)`

My uvicorn invocation is in manage.py, and my app callable (a FastAPI instance) is in main.py, but I can't find any way to specify that the module path that should be loaded is actually app.main. Being able to pass app_dir='app/' to uvicorn.run would then let me specify main:app as the module:callable. Right now, that's impossible as far as I can tell.

@Kludex
Copy link
Sponsor Member

Kludex commented Aug 30, 2021

PR is already created.

Waiting for reviews.

@HansBrende
Copy link
Contributor

HansBrende commented Nov 25, 2021

@command-tab even without this PR, it is not "impossible", you can also easily do the same thing yourself:

sys.path.append('app')  # or possibly something more absolute like os.path.abspath(os.path.join(__file__, '..', 'app'))
uvicorn.run('main:app')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants