Cannot install modules with anaconda installed - python

I installed anaconda a little while ago. Later I installed django. Django was the first program I could install successfully and run without error.
I just tried to install mysqldb with the following:
sudo apt-get install python-mysqldb
and it gave following message:
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-mysqldb is already the newest version.
The following packages were automatically installed and are no longer required:
linux-headers-3.5.0-17 linux-headers-3.5.0-17-generic
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
If I run a django app configure to have a mysql back-end I get
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
I think this has something to do with having anaconda installed. I'm running on ubuntu OS.
Does anyone know how I can get mysqldb module to get recognized by django?
It looks like all my anaconda modules are in:
/home/aiden/anaconda/lib/python2.7
I think my django lives in:
/home/aiden/django/
and i searched for mysqldb and found a file under anaconda:
/home/aiden/anaconda/lib/python2.7/site-packages/sqlalchemy/something.../drizzle/mysqldb.py
I don't know how to check where I installed things to, but I think that mysqldb isn't getting installed b/c it's already somewhere in anaconda

apt-get is going to install things into the system Python. To install things into Anaconda, you need to use the conda package manager, assuming the package is build for conda already, or else build from source. You can also try to find the package on binstar.org if there isn't one in conda already.

I just ran a "conda install bottle" but it didn't have the package available, but it automatically installed bottle using pip. This works fine. Anaconda is great.

Related

Cannot import mysql.connector - ModuleNotFoundError: No module named 'mysql'

import mysql.connector got the following error.
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
However, the following commands show the mysql is installed?
(env) bash-4.2$ pip list | grep mysql
mysql (0.0.3)
mysqlclient (2.0.3)
(env) bash-4.2$ conda list mysql
WARNING conda.core.prefix_data:_load_site_packages(272): Problem reading non-conda package record at lib/python3.6/site-packages/pyzmq-17.0.0-py3.6.egg-info/PKG-INFO. Please verify that you still need this, and if so, that this is still installed correctly. Reinstalling this package may help.
WARNING conda.core.prefix_data:_load_site_packages(272): Problem reading non-conda package record at lib/python3.6/site-packages/jupyterlab-2.3.1-py3.9.egg-info/PKG-INFO. Please verify that you still need this, and if so, that this is still installed correctly. Reinstalling this package may help.
WARNING conda.core.prefix_data:_load_site_packages(272): Problem reading non-conda package record at lib/python3.6/site-packages/conda-4.10.3-py3.6.egg-info/PKG-INFO. Please verify that you still need this, and if so, that this is still installed correctly. Reinstalling this package may help.
WARNING conda.core.prefix_data:_load_site_packages(272): Problem reading non-conda package record at lib/python3.6/site-packages/certifi-2021.5.30-py3.6.egg-info/PKG-INFO. Please verify that you still need this, and if so, that this is still installed correctly. Reinstalling this package may help.
# packages in environment at /apps/external/4/anaconda3:
#
# Name Version Build Channel
mysql 5.7.20 h55eaa98_0 https://mycompany.intranet/repository/anaconda-main-proxy
BTW, why the version of pip mysql is so low? (0.0.3)
pip install mysql-connector
or
Do not make your .py file name: mysql.py or something like that.
OS: Mac 11.6.2 (Big Sur)
Python: 3.9.7
If you, like me, use conda environments, and you have trouble installing the mysql-connector-python module with an existing mysql module installed, try this:
(env)username ~% conda update --all
(env)username ~% conda uninstall mysql
(env)username ~% conda install -c conda-forge mysql
(env)username ~% conda install -c conda-forge mysql-connector-python
Now the mysql.connector package is found from the JupyterLab notebook when running it from either VSCode or from Anaconda Navigator.
I hope this is useful for someone.

pip uninstall fails with "owned by OS" - even under sudo

I'm working on a DevOps project for a client who's using Python. Though I never used it professionally, I know a few things, such as using virtualenv and pip - though not in great detail.
When I looked at the staging box, which I am trying to prepare for running a functional test suite, I saw chaos. Tons of packages installed globally, and those installed inside a virtualenv not matching the requirements.txt of the project. OK, thought I, there's a lot of cleaning up. Starting with global packages.
However, I ran into a problem at once:
➜ ~ pip uninstall PyYAML
Not uninstalling PyYAML at /usr/lib/python2.7/dist-packages, owned by OS
OK, someone must've done a 'sudo pip install PyYAML'. I think I know how to fix it:
➜ ~ sudo pip uninstall PyYAML
Not uninstalling PyYAML at /usr/lib/python2.7/dist-packages, owned by OS
Uh, apparently I don't.
A search revealed some similar conflicts caused by users installing packages bypassing pip, but I'm not convinced - why would pip even know about them, if that was the case? Unless the "other" way is placing them in the same location pip would use - but if that's the case, why would it fail to uninstall under sudo?
Pip denies to uninstall these packages because Debian developers patched it to behave so. This allows you to use both pip and apt simultaneously. The "original" pip program doesn't have such functionality
Update: my answer is relevant only to old versions of Pip. For the latest versions, Pip is configured to modify only the files which reside only in its "home directory" - that is /usr/local/lib/python3.* for Debian. For the latest tools, you will get these errors when you try to delete the package, installed by apt:
For pip 9.0.1-2.3~ubuntu1 (installed from Ubuntu repository):
Not uninstalling pyyaml at /usr/lib/python3/dist-packages, outside environment /usr
For pip 10.0.1 (original, installed from pypi.org):
Cannot uninstall 'PyYAML'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
The point is not that pip cannot install the package because you don't have enough permissions, but because it is not a package installed through pip, so it doesn't want to uninstall it.
dist-packages is where packages installed by the OS package manager reside; as they are handled by another package manager (e.g. apt on Ubuntu/Debian, pacman on Arch, rpm/yum on CentOS, ... ) pip won't touch them (but still has to know about them as they are installed packages, so they can be used to satisfy dependencies of pip-installed packages).
You should also probably avoid to touch them unless you use the correct package manager, and even so, they may have been installed automatically to satisfy the dependencies of some program, so you may not remove them without breaking it. This can usually be checked quite easily, although the exact way depends from the precise Linux distribution you are using.

Python imports wrong version of library outside of virtualenv

Need help updating a python package.
I have an implementation that requires the following import
from twisted.internet.ssl import optionsForClientTLS
"optionsForClientTLS" was added to the twisted framework with version 14(?). I think the non-virtualenv import is getting a dated version-- If that import is within a virtualenv that has twisted installed via pip, everything is fine. Import fails outside the virtualenv.
In the virtualenv
twistd --version
Shows 15.2.1. On the bare system it shows 13.2.0.
pip install twisted
....
pip freeze
shows
Twisted==15.2.1
Uninstalling twisted using pip and reinstalling didn't help. You can install twisted from apt-get using
sudo apt-get install python-twisted
and it installs the older version, but after purging it and installing using only pip I still get the older version.
Possibly related.
I solved the issue in a terrible way. It was a few days ago, so the paths may not be exactly right.
The assumption that there were two python packages was correct (I think.) I suspect the order of the paths in PYTHONPATH meant that the wrong version was being imported first, while pip was installing in a version that was later on in the path.
My "solution" was to copy the twisted directory from the up to date version to the older version. This was either from usr/local/lib/python... to usr/lib/python... or from ... site-packages to ... dist-packages. I can check again if someone has the same issue and can't resolve it.

Version error after pip installing MySQL-python on OSX

I have successfully installed MySQL-python to my virtual environment, confirmed by the fact that PyCharm can import it. I am however getting this message:
ImportError: this is MySQLdb version (1, 2, 4, 'beta', 4), but _mysql is version (1, 2, 5, 'final', 1)
My installation method has been:
Activate my venv in the terminal
export PATH=$PATH:/usr/local/mysql/bin
export CFLAGS=-Qunused-arguments export CPPFLAGS=-Qunused-arguments
pip install MySQL-python
Which returns a successful build. I really don't understand what the problem is, is pip serving me a corrupted directory?
I tried resolving this by googling and the likes to no avail. I then tried downloading version 1.2.4b4 from sourceforge and built it within the venv. This gives me the following error:
ld: warning: ignoring file /usr/local/Cellar/mysql/5.6.19/lib/libmysqlclient_r.dylib, file was built for x86_64 which is not the architecture being linked (i386): /usr/local/Cellar/mysql/5.6.19/lib/libmysqlclient_r.dylib
I have run file $(which ) on mysql and python, returning a 64 bit build for mysql and both a 32 and 64 bit build for python.
I really have no idea what to do next, I would really appreciate some help, let me know if I've missed something! Thank you
EDIT:
I pip uninstalled MySQL-python and tried again with this zip: https://pypi.python.org/pypi/MySQL-python/1.2.5
pip install -Iv https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c
Which gave me the same error as before, stating MySQLdb is version 1.2.4b4 but _mysql is 1.2.5. This leads me to believe I might have a lingering version of MySQL-python that isn't being uninstalled. How would I go about testing this? Thanks!
A couple of common potential issues:
make sure your path is right and that pip and python refer to the same interpreter. Virtual environments are best activated by doing source activate <NAME> or prepending them to the path instead of appending.
I had multiple issues with MySqlSB and stack overflow is full of questions about it. All my problems went away when I switched to pymysql, whose API is fully compatible with mysqldb. Even better, there is a method install_as_MySQLdb() which makes pymysql appear exactly like mysqldb, so all packages the expect the latter work with the former.
The procedure that I use to get set up on OSX is:
get rid of all Python installations (system python, home-brew, etc)
brew install mysql gcc. gcc is in case you want to compile c/fortran extensions to python modules.
Watch for permission errors- always run brew doctor
install anaconda
set up a virtual environment conda create -n <NAME> python=3.4
activate virtual environment source activate <NAME>
install required packages, trying conda first and pip if the package isn't there. For example, I always do conda install numpy so I don't have to compile it myself, and I do pip install pymysql
Thanks for the above answer, good advice for future troubleshooters. I managed to solve this by re-installing the 1.2.5 build and restarting my compiler.
In other words, the method I outlined above should work. For future reference make sure your bin/activate and bin/pip have their paths set to your venv and not someone else on your developer team

Macports does not recognize pip-installed packages

Until today, I have been using the macports version of python27 and installing python packages through macports. Today, I needed some packages which were not available through macports; I learned about pip and found them there. After installing these packages through pip, however, I realized that neither pip nor macports could see what had been installed by the other. So, for consistency, I decided to uninstall all macports packages, install python27 and py27-pip through macports and then proceed to install all of my python packages through pip.
This worked fine, but since macports does not know about my pip-installed python packages, I ran into trouble when installing something else which depends on python (e.g., inkscape): macports tried to install its own version of, e.g. py27-numpy (already installed by pip) and then failed installation because it "already exists and does not belong to a registered port."
Is there a consistent way to use pip and to get macports to recognize that the python packages it might need for something else are already installed?
The solution is: dont use Macports for installing Python's packages.
Macports is a general package manager and it registers installed packages in its database.
Pip is a package manager for Python so if you want to install Python package, use appropriate package management tool. Pip doesnt have it's own database to keep evidence about installed stuff - it just checks Python's path to see if the package is there (and that's what you want).
Sooner or later you'll use Virtualenv anyway and you'll need pip to install packages in there too so it's better to use it everywhere.

Categories

Resources