When im run the Django project in debug mode it's show the module not found error even when the package is installed

I have installed the package ‘drf_yasg’
if im using

python manage.py runserver

it’s working fine. but if I’m running in de-bug mode it’s shows

 File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'drf_yasg'```
![ksnip_20220613-170718|690x394](upload://gt9Ms5bMMfkQ3n9g4zubEcHfV6C.png)

What specifically do you mean by this?

(Generally speaking, the issue is going to be that whatever you’re using to run in “debug mode” is either referencing a different Python environment or doesn’t recognize that the package has been installed.)

error:
ModuleNotFoundError: No module named ‘drf_yasg’
installed pip3 install … drf_yasg

EDIT: No used python from system linux… create virtual envoriment.

python3 -m venv venv
etc…
pip3 install … … …

module: drf-yasg · PyPI

i’am italian my english elementary. studing english from hobby.

If I’m using python manage.py runservrer means it’s working fine but im using python : Django
de-bug means it’s show’s module not found error

here the config json file

ok, no use docker, My use vs code + pylance + minifyall, on raspberry pi server … none idea problem.

Have installed module with link post last?

thanks.

i’m not using docker

i’m using python 3.10.4 version
pip freeze

asgiref==3.5.2
certifi==2022.5.18.1
charset-normalizer==2.0.12
coreapi==2.3.3
coreschema==0.0.4
Django==4.0
djangorestframework==3.13.1
djangorestframework-simplejwt==5.2.0
drf-yasg==1.20.0
idna==3.3
inflection==0.5.1
install==1.3.5
itypes==1.2.0
Jinja2==3.1.2
Markdown==3.3.7
MarkupSafe==2.1.1
openapi-codec==1.3.2
packaging==21.3
psycopg2-binary==2.9.3
PyJWT==2.4.0
pyparsing==3.0.9
pytz==2022.1
requests==2.27.1
ruamel.yaml==0.17.21
ruamel.yaml.clib==0.2.6
simplejson==3.17.6
sqlparse==0.4.2
uritemplate==4.1.1
urllib3==1.26.9```

@luigiamorfini1982 @KenWhitesell
/env/lib/python3.10/site-packages
I have manually added the drf-yasg file in env folder

@KenWhitesell @luigiamorfini1982
lol now i get new error

but while installing psycopg2 i have faced another error

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

but i have installed psycopg2-binary but it’s not recommented for production as per the documentation
please help me to fix that psycopg_documentation

The root cause is that you don’t have VSCode set up to use the same virtual environment for running your application in debug as when you run runserver from the command line.

(That was the cause of your original issue as well - manually copying the library just “masked” the problem, it didn’t solve it.)

Also, on a side note - I, along with many other people I know, do use psycopg2-binary in production modes. There are circumstances where it’s not a good idea to do that, but nothing I do is affected by it.

1 Like

the how to fix this issue

I’m going to have to ask you to please stop posting screenshots. They can’t be quoted or referred to in responses.
Please copy/paste text of messages into your response, surrounded by the lines of three backtick - ` characters, just like you would do with code.

If it’s the same psycopg2 issue as reported earlier, it’s the same issue. You need to make sure that VSCode is using the same virtual environment for debug that you use when you’re running your application using runserver.

I have only one environment it name was env

Yes it’s using the same environment

(env) softsuave@softsuave-H110M-S2:~/project/parking_hero_api$  cd /home/softsuave/project/parking_hero_api ; /usr/bin/env /home/softsuave/project/parking_hero_api/env/bin/python /home/softsuave/.vscode/extensions/ms-python.python-2022.8.0/pythonFiles/lib/python/debugpy/launcher 44409 -- parking_hero/manage.py runserver 
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 25, in <module>
    import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.10/threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 115, in inner_run
    autoreload.raise_last_exception()
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/core/management/__init__.py", line 381, in execute
    autoreload.check_errors(django.setup)()
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/apps/config.py", line 300, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/db/models/base.py", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/db/models/options.py", line 207, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/db/utils.py", line 204, in create_connection
    backend = load_backend(db['ENGINE'])
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/softsuave/project/parking_hero_api/env/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 29, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

No, it’s not. If it were trying to use the same virtual environment, it would be using the same libraries. You’ve got something not configured correctly in your run configuration for debug mode.

I’m not understand
Installed package not showing in environment folder like we disscussed drf-yasg
I don’t know why

if I using pip list it’s show all package i have installed but it’s not shown in env folder

please help

The only explanation I have for this is that the active environment when you run pip list is not the env folder you are looking at.

no Mr @KenWhitesell i have runned in the environment folder, i have restarted my system also just now. i don’t know why it’s happening

Just running something while in the environment directory does not mean that that environment is the active venv. A virtual environment must be activated before it is active.

step 1.use this command

pip freeze > requirements.txt

step2. delete the old env (environment file)

step3. (create the environtment) click here

python3 -m venv /path/to/new/virtual/environment

step4. use this command

pip3 install -r requirements.txt

Ha! I installed psycopg2[binary] and it fixed my issue! Thank you Ken!

Also I am using the Django Forums article searches more and my problems are being solved faster! I AM SO PUMPED! I feel like a real troubleshooter!