Runtime.ImportModuleError: Unable to import module 'vc__handler__python' if the root folder is set one level above the server folder on vercel
I recently watched video “Deploy Django Project on Vercel with Supabase PostgreSQL: Step by Step Guide”. I followed this video step by step, and my project was really deployed. But it deployed if i picked my folder python_server/ as root directory. But I need to select the folder one level up as root directory. I have project structure like this:
Project/
├── python_server/
│ ├── __init__.py
│ ├── mysite/
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ ├── wsgi.py
│ ├── manage.py
│ ├── gif_service/
│ │ ├── templates
│ │ ├── views.py
│ │ ├── urls.py
│ ├── static
│ ├── staticfiles
├── public
├── views
├── js_server
I copied my static files from /public and views from /views to /python_server. And I picked /python_server as folder root. It’s works well for vercel. But it’s not good for me. Because I use views from /views and static files from /public for both server: Express server and Django. And so It’s not good practice to have copy of that static and html files. So I need to deploy vercel not with python_server/ as root folder, but with Project/ as root folder.
I really tried many configuration options but always I get:
and in runtime logs something like:
[ERROR] Runtime.ImportModuleError: Unable to import module 'vc__handler__python': No module named 'mysite' Traceback (most recent call last):INIT_REPORT Init Duration: 1798.90 ms Phase: invoke Status: error Error Type: Runtime.Unknown
or
[ERROR] Runtime.ImportModuleError: Unable to import module 'vc__handler__python': No module named 'django' Traceback (most recent call last):INIT_REPORT Init Duration: 1798.90 ms Phase: invoke Status: error Error Type: Runtime.Unknown
My build_files.sh and vercel.json in python_server/ are like this:
build_files.sh:
#!/bin/bash
pip install setuptools
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py tailwind install
python manage.py collectstatic
python manage.py tailwind start
vercel.json:
{
"version": 2,
"builds": [
{
"src": "mysite/wsgi.py",
"use": "@vercel/python",
"config": { "maxLambdaSize": "15mb",
"runtime": "python3.10",
"buildCommand": "bash build_files.sh"
}
}
],
"routes": [
{
"src": "/(.*)",
"dest": "mysite/wsgi.py"
},
{
"src": "/static/(.*)",
"dest": "/static/$1"
}
]
}
requirements.txt:
asgiref==3.8.1
Django==5.1
django-environ==0.11.2
django-tailwind==3.8.0
psycopg2-binary==2.9.9
sqlparse==0.5.1
typing_extensions==4.12.2
tzdata==2024.1
whitenoise==6.7.0
I already tried to move vercel.json, build_files.sh and requirements.txt to Project/ folder and change "src": "mysite/wsgi.py"
to "src": "python_server/mysite/wsgi.py"
and "dest": "mysite/wsgi.py"
to "dest": "python_server/mysite/wsgi.py"
in vercel.json.
I tried to use build_files.sh like this:
#!/bin/bash
pip install setuptools
pip install -r requirements.txt
python python_server/manage.py makemigrations
python python_server/manage.py migrate
python python_server/manage.py tailwind install
python python_server/manage.py collectstatic
python python_server/manage.py tailwind start
But I got error.
Also I tried to change build_files.sh to this:
#!/bin/bash
cd /python_server
pip install setuptools
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py tailwind install
python manage.py collectstatic
python manage.py tailwind start
I got error again.
I even tried to change "buildCommand": "bash build_files.sh"
to "buildCommand": "cd python_server && bash build_files.sh"
or even change build_files.sh to this:
#!/bin/bash
export PYTHONPATH=$(pwd)/python_server
pip install setuptools
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py tailwind install
python manage.py collectstatic
python manage.py tailwind start
But I always got the same error:
[ERROR] Runtime.ImportModuleError: Unable to import module 'vc__handler__python': No module named 'mysite' Traceback (most recent call last):INIT_REPORT Init Duration: 1798.90 ms Phase: invoke Status: error Error Type: Runtime.Unknown
or
[ERROR] Runtime.ImportModuleError: Unable to import module 'vc__handler__python': No module named 'django' Traceback (most recent call last):INIT_REPORT Init Duration: 1798.90 ms Phase: invoke Status: error Error Type: Runtime.Unknown
Here's how you can answer the question on Stack Overflow with proper formatting:
To resolve the issue [ERROR] Runtime.ImportModuleError: Unable to import module 'vc__handler__python': No module named 'mysite'
, you can follow these steps:
1. Rename build_files.sh
to setup.sh
and update the script:
Rename your build_files.sh
to setup.sh
and update it to include the necessary commands:
#!/bin/bash
# Clear pip cache
pip cache purge
# Install dependencies
pip install setuptools
pip install -r requirements.txt
# Run Django management commands
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic --noinput
2. Update vercel.json
:
In your vercel.json
file, ensure you set the runtime to Python 3.12 and use the setup.sh
script during the build process:
{
"builds": [
{
"src": "DjangoEcommerce/wsgi.py",
"use": "@vercel/python",
"config": {
"maxLambdaSize": "15mb",
"runtime": "python3.12",
"buildCommand": "bash setup.sh"
}
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/DjangoEcommerce/wsgi.py"
},
{
"src": "/static/(.*)",
"dest": "/static/$1"
}
]
}
3. Make setup.sh
executable:
After updating the script, run the following command in your terminal to make setup.sh
executable:
chmod +x setup.sh
4. Verify Database Connection:
Ensure your database is correctly set up and connected, as this can also cause runtime errors if not configured properly.
Explanation:
- Module Import Error: The error usually happens when the module or package cannot be found. By configuring
vercel.json
correctly and ensuring your environment is set up withsetup.sh
, you can avoid this error. - Runtime Configuration: Setting the runtime to Python 3.12 and using the correct build command ensures that your environment is consistent with the expected setup.
This should help resolve the Runtime.ImportModuleError
you are encountering.