skip to Main Content

I have a script that is supposed to start gunicorn:

set -x
cd $VIRTUALENV
. bin/activate
pip freeze
cd $DJANGODIR

### Start Gunicorn

exec gunicorn ${DJANGO_WSGI_MODULE}:application 
        --name $NAME 
        --workers $NUM_WORKERS 
        --user=$USER --group=$GROUP 
        --log-level=debug 
        --bind=127.0.0.1:8000

From the output of it, it seems pretty clear that the virtual env gets activated and the requirement is installed correctly (since the module is displayed when displaying the pip freeze):

+ cd /home/ubuntu/envs/app
+ . bin/activate
++ deactivate nondestructive
++ unset -f pydoc
++ '[' -z '' ']'
++ '[' -z '' ']'
++ '[' -n /bin/bash ']'
++ hash -r
++ '[' -z '' ']'
++ unset VIRTUAL_ENV
++ '[' '!' nondestructive = nondestructive ']'
++ VIRTUAL_ENV=/home/ubuntu/envs/app
++ export VIRTUAL_ENV
++ _OLD_VIRTUAL_PATH=/home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
/usr/games:/usr/local/games:/snap/bin
++ PATH=/home/ubuntu/envs/app/bin:/home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/s
bin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
++ export PATH
++ '[' -z '' ']'
++ '[' -z '' ']'
++ _OLD_VIRTUAL_PS1=
++ '[' x '!=' x ']'
+++ basename /home/ubuntu/envs/app
++ PS1='(app) '
++ export PS1
++ alias pydoc
++ '[' -n /bin/bash ']'
++ hash -r
+ pip freeze
Django==2.2.1
pkg-resources==0.0.0
postgres==2.2.2
psycopg2-binary==2.8.2
python-dotenv==0.10.2
python-memcached==1.59
pytz==2019.1
six==1.12.0
sqlparse==0.3.0
+ cd /home/ubuntu/app/app

+ exec gunicorn app.wsgi:application --name app --workers 3 --user=ubuntu --group=ubuntu --log-le
vel=debug --bind=127.0.0.1:8000
...

Now, this script fails with the following error:

  File "/home/ubuntu/project/app/app/app.py", line 11, in <module>
    from dotenv import load_dotenv, find_dotenv
ImportError: No module named dotenv

Which lead me to suspect that the virtualenv does not remain activated…

If I do the same steps manually, I do not get the error:

cd /home/ubuntu/envs/app
. bin/activate
python
>>> from dotenv import load_dotenv, find_dotenv
>>>

Any pointer will be greatly appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    found the solution: install gunicorn as part of the virtual environment. Typically update the requirements.txt to add the following line:

    gunicorn
    

    Only using the 'gunicorn' installed with the debian package was making it from outside of the virtualenv context.


  2. Looks like the application is not looking for packages in the right place. Try calling sys.path.append() to indicate where libraries are located (need to change the path accordingly to match the environment setup):

    import sys, os
    app_root = os.path.dirname(os.path.realpath(__file__))
    sys.path.append(app_root + '/venv/lib/python3.8/site-packages')
    
    # now you can import the library
    from dotenv import load_dotenv
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search