I tried running my django tests python manage.py test
in gitlab ci. Therefore I'm using a docker image. The docker image builds fine, but when it runs the tests on gitlab i get ImportError: No module named 'django'
and
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?
See my .gitlab-ci.yml
image: registry.gitlab.com/app/core:latest
services:
- postgres:latest
stages:
- test
variables:
SECRET_KEY: test-secret
POSTGRES_DB: ...
POSTGRES_USER: ...
POSTGRES_PASSWORD: ...
python_tests:
stage: test
before_script:
- export DATABASE_NAME=...
- export DATABASE_USER=...
- export DATABASE_PASSWORD=...
- export DATABASE_HOST=postgres
- source /app/venv/bin/activate
script:
- python manage.py test
and Dockerfile
FROM ubuntu:16.04
RUN apt-get update -y -qq
RUN apt-get install -y -qq build-essential libffi-dev libpq-dev libfontconfig1
RUN apt-get install -y -qq python3 python3-dev python3-pip
RUN apt-get install -y -qq libpq-dev
RUN apt-get install -y -qq nodejs npm
WORKDIR /app
# pip
COPY requirements.txt /app
RUN pip3 install --upgrade pip
RUN pip3 install virtualenv
RUN virtualenv --no-site-packages venv
RUN . venv/bin/activate
RUN pip3 install -r /app/requirements.txt
# npm
RUN ln -s `which nodejs` /usr/bin/node
COPY web/vueapp/package.json /app
RUN npm install
Well you have a issue in your approach. Consider your below RUN statements in Dockerfile
RUN . venv/bin/activate
RUN pip3 install -r /app/requirements.txt
Both above statements are like open two terminals, in one execute . venv/bin/activate
. venv/bin/activate
and in one pip3 install -r /app/requirements.txt
So your environment gets activated and you let it close and then the next pip3 statement installs on global packages. So change your code to below
RUN . venv/bin/activate && pip3 install -r /app/requirements.txt
When you script is running your are activate the environment and then running python manager.py inside the virtual environment which is blank with no packages. So above change should fix that issue for you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.