[PYTHON] If you can't import even though it is already installed by pip

It is installed when pip is installed, but it cannot be imported. It's a simple story, but for example

>>> import django
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'django'

I get an error when I try to import django. The content of the error remains the same, but the module is not found.

I wondered if there was no django in the first place, and when I piped it,

C:\Users\aaa>pip install django
Requirement already satisfied: django in c:\users\aaa\anaconda3\lib\site-packages (3.0)
Requirement already satisfied: sqlparse>=0.2.2 in c:\users\aaa\anaconda3\lib\site-packages (from django) (0.3.0)
Requirement already satisfied: asgiref~=3.2 in c:\users\aaa\anaconda3\lib\site-packages (from django) (3.2.3)
Requirement already satisfied: pytz in c:\users\aaa\anaconda3\lib\site-packages (from django) (2019.3)

Since it is already satisfied, it is said that it is already satisfied. That simply means that the path doesn't go through.

You can check the path in sys.path

>>> import sys
>>> import path
>>> pprint.pprint(sys.path)
['',
 'C:\\Users\\aaa\\Anaconda3\\python37.zip',
 'C:\\Users\\aaa\\Anaconda3\\DLLs',
 'C:\\Users\\aaa\\Anaconda3\\lib',
 'C:\\Users\\aaa\\Anaconda3',
 'C:\\Users\\aaa\\Anaconda3\\lib\\site-packages',
 'C:\\Users\\aaa\\Anaconda3\\lib\\site-packages\\win32',
 'C:\\Users\\aaa\\Anaconda3\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\aaa\\Anaconda3\\lib\\site-packages\\Pythonwin',

Sure, I don't have the django path "django in c: \ users \ aaa \ anaconda3 \ lib \ site-packages (3.0)".

Once you know this, all you have to do is add the path to your environment variables. You can add it to an environment variable with sys.path.append,

sys.path.append("c:/users/aaa/anaconda3/lib/site-packages (3.0)")

If you thrust it in, check it again

>>> pprint.pprint(sys.path)
 'c:/users/aaa/anaconda3/lib/site-packages (3.0)'

Is OK if is added.

>>> import django
>>> print(django.get_version())
3.0

I was able to confirm that django can be used.

Error pattern

By the way, when I try to copy and paste it into an environment variable, I get this error.

>>> sys.path.append("c:\users\aaa\anaconda3\lib\site-packages (3.0)")
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape
>>> sys.path.append("c:/users/aaa/anaconda3/lib/site-packages (3.0)")

This is because the \ mark is an escape, so \ u recognizes it as an escape sequence. sys.path.append ("c: \ users \ aaa \ anaconda3 \ lib \ site-packages (3.0)") You can do it with sys.path.append ("c: /users/aaa/anaconda3/lib/site-packages (3.0)").

Recommended Posts

If you can't import even though it is already installed by pip
Import error even though python is installed
If you should have pip installed but get an Import error
If you try to install Python2 pip after installing Python3 pip and it is rejected
What to do if you can't pip install mysqlclient
What is pip and how do you use it?
What to do if you get angry with "Gtk * backend requires pygtke to be installed" even though pygtk is included in matplotlib
If you get Error: That port is already in use. In Django
What to do if you can't install pyaudio with pip #Python
Tensorflow with Raspberry Pi. If you stumble with pip, solve it with virtualenv!
[TensorFlow] If you want to run TensorBoard, install it with pip