kokserek
kokserek

Reputation: 589

Suddenly ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable?

Suddenly my Django project showed me the following ImportError: "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?".

It ran fine before.

I work on Mac m1 using PyCharm Professional.

My Django is installed. My venv is activated. So, I guess I have problem with PYTHONPATH .

My interpreter is exactly I would like to use: My interpreter

Actually I have some versions of Python. which Python shows me:

(venv) user_name@computer_name project_name % which python
python: aliased to /usr/bin/python3
(venv) user_name@computer_name project_name % which python3
/Users/user_name/PyCharmProjects/TrainingProjects/project_name/venv/bin/python3
(venv) user_name@computer_name project_name % which python3.9
/opt/homebrew/bin/python3.9

python3 -c "import sys; print(sys.path)" shows me:

/opt/homebrew/Cellar/[email protected]/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python310.zip
/opt/homebrew/Cellar/[email protected]/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10
/opt/homebrew/Cellar/[email protected]/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload
/Users/user_name/PyCharmProjects/TrainingProjects/project_name/venv/lib/python3.10/site-packages

If I understand this output correctly, it means that there is no python 3.9 in my PYTHONPATH for now.

In my settings.py, there are:

import os
import sys #not active
from pathlib import Path

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
which $PYTHONPATH 

brings me PYTHONPATH not found.

echo $PYTHONPATH

brings me nothing.

The export:

export PYTHONPATH=$PYTHONPATH:/Library/Frameworks/Python.framework/Versions/3.9/bin/python3

didn't help.

Upvotes: 0

Views: 1595

Answers (1)

JL Peyret
JL Peyret

Reputation: 12204

OK, I dunno if writing an answer is a good approach, but I may have some suggestions and I need a clean slate to write it in. So...

First, let's forget about Python 3.9 and Python 3.10 and PYTHONPATH for a sec.

Second, let's try to find where django is installed. This looks for a bin that django uses/delivers.

mdfind -name django-admin.py

which returned

/Users/me/kds2/issues2/500.macos/010.fixed.p1.bin_old_py/obsolete/[email protected]/django-admin.py
👉/Users/me/kds2/venvs/bme/bin/django-admin.py 1️⃣
👉/Users/me/kds2/venvs/bme/lib/python3.10/site-packages/django/bin/django-admin.py 2️⃣
/usr/local/lib/python3.8/site-packages/django/bin/django-admin.py

I know my own environment, so I know what's interesting are the 2 under /venvs/bme.

In fact, I know 1️⃣, in /bin/ basically loads 2️⃣ in /site-packages/

Your django will be structured the same way, just in another location.

From your posting so far, I assume your own venv is at

/Users/user_name/PyCharmProjects/TrainingProjects/project_name/venv

So, cd to its bin (which is also probably where you ran source activate)

cd /Users/user_name/PyCharmProjects/TrainingProjects/project_name/venv/bin

type the following command

ls -l

This is what it gets me (after I trim off some files from the output):

-rw-r--r--   1 me  staff  1991  8 Jan  2022 activate
-rw-r--r--   1 me  staff   917  8 Jan  2022 activate.csh
-rw-r--r--   1 me  staff  2059  8 Jan  2022 activate.fish
-rwxr-xr-x   1 me  staff   242 24 Jun 14:04 pip
-rwxr-xr-x   1 me  staff   242 24 Jun 14:04 pip3
-rwxr-xr-x   1 me  staff   242 24 Jun 14:04 pip3.10
lrwxr-xr-x   1 me  staff    21  8 Jan  2022👉python -> /opt/local/bin/python
lrwxr-xr-x   1 me  staff     6  8 Jan  2022 python3 -> python
lrwxr-xr-x   1 me  staff     6  8 Jan  2022👉python3.10 -> python

OK, the important this is that these are symlinks pointing to the actual Python that is driving/hosting this venv. My python is pointing to /opt/local/bin/python because I installed via macports but yours ought to be pointing to homebrews.

Maybe /opt/homebrew/Cellar/[email protected] ?

And my venv is using Python3.10, as you can see. I expect yours will be as, rather than 3.9.

OK, now let's see what that venv has set up as its environment?

./python3 -m site doc about command

Notice the leading dot. What this does is ignore PATH and whatever and just run this local python3.

What it outputs for me is a whole of useful information, about this virtualenv.

sys.path = [
    '/Users/me/kds2/venvs/bme/bin',
    '/Users/me/Library/Python/3.10/lib/python/site-packages/_pdbpp_path_hack',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload',
    '/Users/me/kds2/venvs/bme/lib/python3.10/site-packages', 👈
    '/Users/me/kds2/mygithub/pynoorm',
    '/Users/me/kds2/mygithub/bmedev',
    '/Users/me/kds2/mygithub/pip_stripper',
    '/Users/me/kds2/py2',
    '/Users/me/kds2/mygithub/lazy-regression-tests/lazy_regression_tests',
    '/Users/me/Library/Python/3.10/lib/python/site-packages',
    '/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages',
]
USER_BASE: '/Users/me/Library/Python/3.10' (exists)
USER_SITE: '/Users/me/Library/Python/3.10/lib/python/site-packages' (exists)
ENABLE_USER_SITE: True

That sys.path list above is location at which import will try to find things. Look for ones ending with /site-packages`.

And for those, try to see if there is a django package. Like this.

ls -d /Users/me/kds2/venvs/bme/lib/python3.10/site-packages/django

Sure enough, in the same location where I found my django-admin before.

/Users/jluc/kds2/venvs/bme/lib/python3.10/site-packages/django

Now, I don't know how pycharm and 3.9 figure into this, but that should give a little bit of an idea how your venv is put together.

Maybe you'd be better off reinstalling another venv with django and starting afresh. Your code wouldn't affected. Your call.

Upvotes: 1

Related Questions