Search code examples
pythonamazon-elastic-beanstalk

Elastic Beanstalk importError


module not found error django ImportError

Error log

2023-12-24 12:02:34,096 P2120 [INFO] Command 01_collectstatic
2023-12-24 12:02:34,133 P2120 [INFO] -----------------------Command Output-----------------------
2023-12-24 12:02:34,133 P2120 [INFO]    Traceback (most recent call last):
2023-12-24 12:02:34,133 P2120 [INFO]      File "/var/app/staging/manage.py", line 11, in main
2023-12-24 12:02:34,133 P2120 [INFO]        from django.core.management import execute_from_command_line
2023-12-24 12:02:34,133 P2120 [INFO]    ModuleNotFoundError: No module named 'django'
2023-12-24 12:02:34,133 P2120 [INFO]    
2023-12-24 12:02:34,133 P2120 [INFO]    The above exception was the direct cause of the following exception:
2023-12-24 12:02:34,133 P2120 [INFO]    
2023-12-24 12:02:34,133 P2120 [INFO]    Traceback (most recent call last):
2023-12-24 12:02:34,133 P2120 [INFO]      File "/var/app/staging/manage.py", line 22, in <module>
2023-12-24 12:02:34,133 P2120 [INFO]        main()
2023-12-24 12:02:34,133 P2120 [INFO]      File "/var/app/staging/manage.py", line 13, in main
2023-12-24 12:02:34,134 P2120 [INFO]        raise ImportError(
2023-12-24 12:02:34,134 P2120 [INFO]    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?
2023-12-24 12:02:34,134 P2120 [INFO] ------------------------------------------------------------
2023-12-24 12:02:34,134 P2120 [ERROR] Exited with error code 1

but,install success log eb-engine

Successfully built django-allauth
Installing collected packages: webencodings, urllib3, sqlparse, soupsieve, six, python-ipware, pyjwt, pycparser, psycopg2-binary, Pillow, oauthlib, idna, django-widget-tweaks, django-environ, defusedxml, charset-normalizer, certifi, asgiref, requests, python3-openid, django-ipware, Django, cffi, bleach, beautifulsoup4, requests-oauthlib, django-ckeditor-5, cryptography, django-allauth

Successfully installed Django-4.2.4 Pillow-10.1.0 asgiref-3.7.2 beautifulsoup4-4.12.2 bleach-5.0.1 certifi-2023.11.17 cffi-1.16.0 charset-normalizer-3.3.2 cryptography-41.0.7 defusedxml-0.7.1 django-allauth-0.55.2 django-ckeditor-5-0.2.10 django-environ-0.11.2 django-ipware-6.0.0 django-widget-tweaks-1.5.0 idna-3.6 oauthlib-3.2.2 psycopg2-binary-2.9.9 pycparser-2.21 pyjwt-2.8.0 python-ipware-2.0.1 python3-openid-3.2.0 requests-2.31.0 requests-oauthlib-1.3.1 six-1.16.0 soupsieve-2.5 sqlparse-0.4.4 urllib3-2.1.0 webencodings-0.5.1

Why?

I found by tutorial

    PYTHONPATH: "/var/app/current:$PYTHONPATH"

but, error.

eb ssh

echo $PYTHONPATH
no output

directory structure

root => var
var]$ cd app
[ec2-user@ip-app]$ cd venv/
[ec2-user@ip- venv]$ cd staging-LQM1lest/
[ec2-user@ip- staging-LQM1lest]$ ls
bin  include  lib  lib64  pyvenv.cfg
[ec2-user@ip- staging-LQM1lest]$ cd lib
[ec2-user@ip- lib]$ ls
python3.11
[ec2-user@ip- lib]$ cd python3.11/
[ec2-user@ip- python3.11]$ ls
site-packages
[ec2-user@ip- python3.11]$ cd site-packages/

other

    PYTHONPATH: "/var/app/venv/staging-LQM1lest/"

error

echo $PYTHONPATH
no output

What should I do?

P.S My config file:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: my-project.wsgi:application
  aws:elasticbeanstalk:application:environment:
    PYTHONPATH: "xxx"

  aws:elb:listener:443:
    SSLCertificateId: arn:aws:acm:xxxxxxx
    ListenerProtocol: HTTPS
    InstancePort: 80

container_commands:
  01_collectstatic:
    command: 'python3 manage.py collectstatic --noinput'

It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details.


Solution

  • container_commands:
      01_collectstatic:
        command: "source /var/app/venv/*/bin/activate && python3 manage.py collectstatic --noinput"
    
    

    it worked!

    I don't know the details. But I understand the command was executed before the virtual environment became active.