3

I am running into the following issue trying to install mysqlclient as part of getting a Django project up and running on an AWS EC2 instance.

In a python 3.8.5 virtual environment:

(venv3)$ which pip
~/venv3/bin/pip

(venv3)$ pip --version
pip 21.0.1 from /home/ec2-user/venv3/local/lib/python3.8/dist-packages/pip (python 3.8)

(venv3)$ pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.0.3-cp38-cp38-linux_x86_64.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.0.3

I try running the Django Shell:

(venv3)$ python manage.py shell
...full trace stack truncated for brevity...
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

So I tried to find where pip installed it, and it seems to not actually be installed or be a trace of it anywhere:

(venv3)$ pip show mysqlclient
WARNING: Package(s) not found: mysqlclient

(venv3)$ pip freeze | grep -i mysqlclient
<nothing>

(venv3)$ sudo find / -name mysqlclient
<nothing>

Then as a sanity check I apparently successfully install it but then pip can't find it to uninstall it:

(venv3)$ pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.0.3-cp38-cp38-linux_x86_64.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.0.3

(venv3)$ pip uninstall mysqlclient
WARNING: Package(s) not found: mysqlclient

Other things I have tried/verified

  • Making sure python is the 64-bit version
  • Checking pip outside of the virtual environment
  • Nuking the virtual environment and starting again
  • Trying to install specific versions of mysqlclient
  • Installing and importing/using any other python package

EDIT: Wanted to add that I have also deleted the pip cache and it still does not work.

liamhawkins
  • 1,301
  • 2
  • 12
  • 30
  • maybe try `pip install --upgrade --no-deps --force-reinstall mysqlclient ` – Jay Mody Feb 15 '21 at 16:54
  • Same output, gives installation success message but it's no where to be found – liamhawkins Feb 15 '21 at 16:55
  • what happens with `python -c "import mysqlclient"`? Can you use it to connect to your database outside of django? are you sure you have a your database information and credentials properly specified? – Paul H Feb 15 '21 at 17:05
  • ModuleNotFoundError. Database information/credentials are fine, they work on this machine and locally. This is just an issue getting Django/python3 to connect to mysql – liamhawkins Feb 15 '21 at 17:12
  • 1
    Try running pip in verbose mode. Does it log anything? – user202729 Feb 16 '21 at 06:05
  • @user202729 It logged nothing out of the ordinary, but your comment led to my solution, thanks!! – liamhawkins Feb 19 '21 at 19:59

1 Answers1

1

Answering my own question because I finally figured it out.

As per @user202729's comment I ran pip in verbose mode pip install mysqlclient --verbose which didn't itself show anything out of the ordinary as far as I could tell, but did show that mysqlclient and MySQLdb related files were being installed in /dist-packages which was not being picked my by my python installation. I copied the MySQLdb directory from venv3/lib64/python3.8/dist-packages to venv3/lib64/python3.8/site-packages and that solved the python and django issues.

liamhawkins
  • 1,301
  • 2
  • 12
  • 30
  • "copy"? That will definitely solve the problem (as well as manually extract the source files into the correct directory), but it's still not clear what's the underlying issue. – user202729 Feb 20 '21 at 04:07
  • With some searching there's https://stackoverflow.com/questions/9387928/whats-the-difference-between-dist-packages-and-site-packages (it doesn't explain much), but it's likely because of some environment variable, pip configuration or misconfigured PYTHONPATH. – user202729 Feb 20 '21 at 04:08
  • I figured it was one of the two but had been banging my head against the wall long enough that this works for now. I came across the same question which only inspired the solution and did not explain it. – liamhawkins Feb 20 '21 at 17:19