problem installing packages with pipenv

Hi everyone
I apologize in advance if my question is not relevant to this forum but it made me really crazy and i am struggling with it for almost a week searching for solutions in the internet but none of them works for me
so here is my problem:
whatever package i install with pipenv it installs without any problem and add to Pipfile and Pipfile.lock
and in my code i can import libraries of that package and the IDE detect the package but when i run server it says No module named ‘some module’
to add some more details i ran pip freeze in virtual env and reportlab was there
so any help would be really appreciated
Thanks

I’d say the first thing to check would be to ensure that your virtual environment is active when your server is being started.

Beyond that, we’d need a lot more details including the specifics of how you’re running your server and the full traceback of the error you’re receiving to even begin helping you to diagnose this.

1 Like

Hello Mohammad
I think your virtual environment is not active
so first check if it’s active or maybe env file is not in the same project directory
1-pipenv install -r requirements.txt
then
2-pipenv shell
then try to run your server and if still run into the same issue can you provide us with error screenshot

@Mu5alaf

Side note: We discourage screenshots for submitting information except when absolutely necessary.
It’s a lot more useful to copy/paste the text of the traceback into the body of the post (surrounded by the lines of ``` as appropriate).

1 Like

Yes Mr. ken you’re right

actually i dockerised my project so whenever i want to install a package first i run docker in detach mode
then run pipenv install
now here is the track back of my system after running those commands

mohammad@mohammad-HP-ZBook-15v-G5:/media/mohammad/Local Disk/Projects/Django projects/code/store$ docker-compose up -d
/usr/lib/python3/dist-packages/paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
  "class": algorithms.Blowfish,
Starting store_db_1 ... done
Starting store_web_1 ... done
mohammad@mohammad-HP-ZBook-15v-G5:/media/mohammad/Local Disk/Projects/Django projects/code/store$ docker-compose exec web pipenv install reportlab
/usr/lib/python3/dist-packages/paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
  "class": algorithms.Blowfish,
Creating a virtualenv for this project...
Pipfile: /code/Pipfile
Using /usr/local/bin/python3 (3.10.13) to create virtualenv...
⠦ Creating virtual environment...created virtual environment CPython3.10.13.final.0-64 in 847ms
  creator CPython3Posix(dest=/root/.local/share/virtualenvs/code-_Py8Si6I, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==23.3.1, setuptools==69.0.2, wheel==0.42.0
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/code-_Py8Si6I
Installing reportlab...
Resolving reportlab...
Added reportlab to Pipfile's [packages] ...
✔ Installation Succeeded
Pipfile.lock (149c8a) out of date, updating to (3ded01)...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
⠇ Locking...Warning: WARNING:pipenv.patched.pip._vendor.urllib3.connectionpool:Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/weasyprint/
Locking [dev-packages] dependencies...
Updated Pipfile.lock (cea7da051bd5b5b14623f110c6519906bc31cff224de98e9972ea970063ded01)!
Installing dependencies from Pipfile.lock (3ded01)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
mohammad@mohammad-HP-ZBook-15v-G5:/media/mohammad/Local Disk/Projects/Django projects/code/store$ docker-compose exec web pipenv run pip install reportlab
/usr/lib/python3/dist-packages/paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
  "class": algorithms.Blowfish,
Requirement already satisfied: reportlab in /root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.10/site-packages (4.0.9)
Requirement already satisfied: pillow>=9.0.0 in /root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.10/site-packages (from reportlab) (10.2.0)
Requirement already satisfied: chardet in /root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.10/site-packages (from reportlab) (5.2.0)

so far so good
then i import reportlab module and here comes the error

mohammad@mohammad-HP-ZBook-15v-G5:/media/mohammad/Local Disk/Projects/Django projects/code/store$ docker-compose up
/usr/lib/python3/dist-packages/paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
  "class": algorithms.Blowfish,
Creating network "store_default" with the default driver
Creating store_db_1 ... done
Creating store_web_1 ... done
Attaching to store_db_1, store_web_1
db_1   | 
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   | 
db_1   | 2024-01-15 18:16:14.704 UTC [1] LOG:  starting PostgreSQL 15.5 (Debian 15.5-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
db_1   | 2024-01-15 18:16:14.704 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2024-01-15 18:16:14.704 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2024-01-15 18:16:14.709 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2024-01-15 18:16:14.717 UTC [29] LOG:  database system was shut down at 2024-01-15 18:16:08 UTC
db_1   | 2024-01-15 18:16:14.728 UTC [1] LOG:  database system is ready to accept connections
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  | 
web_1  | Exception in thread django-main-thread:
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
web_1  |     self.run()
web_1  |   File "/usr/local/lib/python3.10/threading.py", line 953, in run
web_1  |     self._target(*self._args, **self._kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 133, in inner_run
web_1  |     self.check(display_num_errors=True)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 485, in check
web_1  |     all_issues = checks.run_checks(
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
web_1  |     new_errors = check(app_configs=app_configs, databases=databases)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 14, in check_url_config
web_1  |     return check_resolver(resolver)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 24, in check_resolver
web_1  |     return check_method()
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 494, in check
web_1  |     for pattern in self.url_patterns:
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 57, in __get__
web_1  |     res = instance.__dict__[self.name] = self.func(instance)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 715, in url_patterns
web_1  |     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 57, in __get__
web_1  |     res = instance.__dict__[self.name] = self.func(instance)
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 708, in urlconf_module
web_1  |     return import_module(self.urlconf_name)
web_1  |   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
web_1  |     return _bootstrap._gcd_import(name[level:], package, level)
web_1  |   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
web_1  |   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
web_1  |   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
web_1  |   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
web_1  |   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
web_1  |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
web_1  |   File "/code/config/urls.py", line 31, in <module>
web_1  |     path('orders/', include('orders.urls')),
web_1  |   File "/usr/local/lib/python3.10/site-packages/django/urls/conf.py", line 38, in include
web_1  |     urlconf_module = import_module(urlconf_module)
web_1  |   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
web_1  |     return _bootstrap._gcd_import(name[level:], package, level)
web_1  |   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
web_1  |   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
web_1  |   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
web_1  |   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
web_1  |   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
web_1  |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
web_1  |   File "/code/orders/urls.py", line 2, in <module>
web_1  |     from .views import BuyBasketView, pdf_view
web_1  |   File "/code/orders/views.py", line 9, in <module>
web_1  |     import reportlab
web_1  | ModuleNotFoundError: No module named 'reportlab'

the problem would not finish here .whatever package i try to install same error happens

Please post your docker-compose file, and any configuration files that it references.

docker-compose.yml

version: '3.8'
services:
 web:
  build: .
  command: python /code/manage.py runserver 0.0.0.0:8000
  volumes:
   - .:/code
  ports:
   - 8000:8000
  depends_on:
   - db
  environment:              
   - "DJANGO_SECRET_KEY=a5a3=3^rn$$gb0==@wy3d2jxrd^pro!4=0+jrb-9fyu0n%z5=jo" 
   - "DJANGO_DEBUG=True"
   - POSTGRES_PASSWORD='123456'
   - POSTGRES_USER=postgres   
 db:
  image: postgres:15
  volumes:
   - postgres_data:/var/lib/postgresql/data/
  environment:
   - "POSTGRES_HOST_AUTH_METHOD=trust"

volumes:
 postgres_data:
  external: true

There is nothing in your compose file that indicates that you are going to use the virtual environment in that container.

What is the docker file for your “web” container?

Side note: If you’re looking to produce a “production-quality” docker container for your Django app, this is not the way to do it. The runserver command is not appropriate for production use.

Quoting directly from the docs for runserver

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests.

If you’re just doing this as an educational exercise, it’s probably ok. But if this is going to be something you want to rely upon, this most definitely is not the way to do it.

dockerfile

# Pull base image
FROM python:3.10

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system

# Copy project
COPY . /code/

EXPOSE 8000

There’s no need to use a virtual environment in a container such as this.

Add your other installation commands into your dockerfile so that everything is installed and ready to go.

Then install either gunicorn or uwsgi to make it a proper container for use.

Also add your command to run gunicorn in the container using the CMD directive.

Thanks
I am going to give it a try
i will let you know what the result is

so could you tell me more specific what to do

  • Add the commands into your dockerfile to “pip install” all the packages your project needs, including gunicorn

  • Add a CMD directive to run gunicorn

  • Don’t run any commands to install packages in a virturalenv. Don’t create a virtualenv. Don’t try to use a virtualenv.

you mean the way i formed my project with dockerfile, docker-compose.yml, pipfile and pipfile.lock is redundant and i can pack it up in one docker file?

You still need the dockerfile to create the container. You still need the docker-compose to run it. It’s the other stuff you don’t need.

alright thanks again
i am going to use your advice in action