ChristopherGS / ultimate-fastapi-tutorial

The Ultimate FastAPI Tutorial
974 stars 334 forks source link

Unable to run the server on Windows #28

Open eryk-mazus opened 2 years ago

eryk-mazus commented 2 years ago

Hi,

I've tried to follow the instructions in README.md and I got stuck at step 3. When I type poetry run ./run.sh in the terminal I get the following error:

  OSError

  [WinError 193] %1 is not a valid Win32 application

  at ~\anaconda3\lib\subprocess.py:1420 in _execute_child
      1416│             sys.audit("subprocess.Popen", executable, args, cwd, env)
      1417│
      1418│             # Start the process
      1419│             try:
    → 1420│                 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
      1421│                                          # no special security
      1422│                                          None, None,
      1423│                                          int(not close_fds),
      1424│                                          creationflags,

I'm new to a poetry and I wonder whether it caused by me using windows or anacoda prompt ? Cheers

Edit: I've managed to run it with poetry run python ./app/main.py command although I'm not sure if everything is setup correctly that way.

mxblsdl commented 2 years ago

Ran into the same issue on step part 7 where the DB is initialized with the command poetry run ./prestart.sh. I ended up running each of the commands in ./prestart.sh from a console separately and that appears to have worked. I would try the same with run.sh

Also running Windows.

cording12 commented 2 years ago

So I think I've come up with a solution that removes the use of prestart.sh but retains the functionality.

From what I can tell, this seems to be a problem unique to Windows 10 that I couldn't find a particular reason for in this specific situation we're experiencing. It can be anything from your Python installation to a problem with a specific package that's been installed.

I've worked around it by creating a new file, named prestart.py and placing it in a new package directory named exec_scripts. Located as follows:

└── app
    ├── alembic
    ├── app
    │   ├── crud
    │   ├── db
    │   └── exec_scripts <--- NEW
    │       ├── __init__.py
    │       └── prestart.py
    └── pyproject.toml <--- MODIFIED

The contents of my prestart.py is as following:

from backend_pre_start import main as pre_start
from initial_data import main as init_data
import alembic.config

def preconfig():
    pre_start()
    alembic.config.main(argv=['revision', '--autogenerate'])
    alembic.config.main(argv=['upgrade', 'head'])
    init_data()

I have then updated pyproject.toml as follows:

[tool.poetry]
name = "Your project name"
version = "0.1.0"
description = ""
authors = ["Author name"]

# Added packages
packages = [
    {include = "exec_scripts", from = "app"}
]

[tool.poetry.dependencies]
python = "^3.10"
fastapi = {extras = ["all"], version = "*"}
uvicorn = "*"
python-multipart = "*"
pydantic = "*"
jinja2 = "*"
sqlalchemy = "*"
black = "*"
tenacity = "*"
alembic = "*"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

# Added below script
[tool.poetry.scripts]
ps = "app.exec_scripts.prestart:preconfig"

Once that's done run a poetry install to pickup the new scripts.

This command can now be run by doing poetry run ps

The same can be applied to the run.sh file using the same method.

Note: I also found that without the alembic revision --autogenerate the tables won't generate with just alembic upgrade head command alone