App Runner Django Deployment Error Python Version3.11

0

I'm trying to deploy my Django backend to AppRunner.

My apprunner.yml is this;

`

version: 1.0
runtime: python311
build:
  commands:
    build:        
      - pip3 install pipenv
      - pipenv install
    post-build:
      - python3 manage.py test
  env:
    - name: DEBUG
      value: "on"
    - name: SECRET_KEY
      value: *****
    - name: ALLOWED_HOSTS
      value: "*"
run:
  runtime-version: 3.11
  pre-run:  
    - pip3 install pipenv
    - pipenv install
  command: pipenv run gunicorn django_apprunner.wsgi --log-file -
  network: 
    port: 8000
    env: 8000  
  env:
    - name: DEBUG
      value: "on"
    - name: SECRET_KEY
      value: ****
    - name: ALLOWED_HOSTS
      value: "*"

In the Application Log File this error comes;

 raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

I could not find any solution for this, is there any idea to fix this issue ?

Taha
asked a month ago181 views
3 Answers
0
Accepted Answer

Why I really do not know, I run the build code under the pre-run and successfully deploy and run

version: 1.0
runtime: python311
build:
  commands:
    build:
      - python3.11 -m ensurepip --upgrade
      - python3.11 -m pip install Django
      - python3.11 -m pip install -r requirements.txt
      - python3.11 -m pip install gunicorn
  env:
    - name: DEBUG
      value: "on"
    - name: SECRET_KEY
      value: "*"
    - name: ALLOWED_HOSTS
      value: "*"
run:
  runtime-version: 3.11
  pre-run:
    - pip3 install gunicorn
    - pip3 install -r requirements.txt
    - which gunicorn
  command: gunicorn app_runne.wsgir:application  --log-file -
  network: 
    port: 8000
    env: 8000  
  env:
    - name: DEBUG
      value: "on"
    - name: SECRET_KEY
      value: "*"
    - name: ALLOWED_HOSTS
      value: "*"

Taha
answered a month ago
profile picture
EXPERT
reviewed 25 days ago
0

Hello, I also faced the same issue but somehow I resolved this issue. I was testing AWS App Runner for my security research work in post compromise scenario. Here's the blog.

  • App Runner doesn't natively manage virtual environments like pipenv. You need to ensure Django and its dependencies are installed within the build container itself.
  • Remove the pipenv install commands from both build and pre-run sections. They're not necessary in this context.
  • Include a command that installs Django and required dependencies directly using pip3 install Django (replace Django with your specific package names) in the build section.

Use the below YAML code once and check if pip is also upgraded. Hope this helps!

version: 1.0
runtime: python311
build:
  commands:
    - python3.11 -m ensurepip --upgrade # Upgrade pip (optional)
    - pip3 install Django # Install Django and dependencies
    - python3 manage.py test
  env:
    - name: DEBUG
      value: "on"
    - name: SECRET_KEY
      value: *****
    - name: ALLOWED_HOSTS
      value: "*"
run:
  runtime-version: 3.11
  command: pip3 install gunicorn django_apprunner.wsgi # Install gunicorn at runtime
  pre-run: [] # Remove pre-run section
  command: gunicorn django_apprunner.wsgi --log-file -
  network: 
    port: 8000
    env: 8000  
  env:
    - name: DEBUG
      value: "on"
    - name: SECRET_KEY
      value: ****
    - name: ALLOWED_HOSTS
      value: "*"
profile picture
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • Invalid start command. Update the start command you provided during the App Runner service configuration and re-try. - CannotStartContainerError: ResourceInitializationError: failed to create new container runtime task: failed to create shim task: failed to create task: rpc error: code = Unknown desc = OCI runtime create failed: runc create failed: unable to start container process: exec: "gunicorn": executable file not found in $PATH: unknown

    After refactoring the yml, this error occurs ? Do you have any other suggestion ?

0

yeah please try this

Install Gunicorn During Build: Ensure Gunicorn is installed during the build phase using pip3 install gunicorn. This ensures it's available in the container's environment.

Check File Paths and Permissions: Double-check that the path to django_apprunner.wsgi is correct and that the file has execute permissions.

version: 1.0
runtime: python311
build:
  commands:
    - python3.11 -m ensurepip --upgrade
    - pip3 install Django # Install Django and dependencies
    - pip3 install gunicorn # Install gunicorn during build
    - python3 manage.py test
  env:
    - name: DEBUG
      value: "on"
    - name: SECRET_KEY
      value: *****
    - name: ALLOWED_HOSTS
      value: "*"
run:
  runtime-version: 3.11
  command: gunicorn django_apprunner.wsgi --log-file -
  network:
    port: 8000
    env: 8000
  env:
    - name: DEBUG
      value: "on"
    - name: SECRET_KEY
      value: ****
    - name: ALLOWED_HOSTS
      value: "*"
profile picture
answered a month ago
  • Unfortunately, the same error occurs, and the file PATH is true but I do not understand how can I give permission to this file.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions