I'm new to Python and I'm trying to run a Python2 script on a Mac that depends on a module, which I have installed but according to the script can't be found. Why is the following failing?
Home:Dev m$ python -V
Python 2.7.10
Home:Dev m$ sudo pip install coincurve==5.2.0 pysha3
Password:
The directory '/Users/m/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/m/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting coincurve==5.2.0
Downloading coincurve-5.2.0-cp27-cp27m-macosx_10_6_intel.whl (157kB)
100% |████████████████████████████████| 163kB 1.3MB/s
Collecting pysha3
Downloading pysha3-1.0.2.tar.gz (829kB)
100% |████████████████████████████████| 829kB 692kB/s
Requirement already satisfied: cffi>=1.3.0 in /usr/local/lib/python2.7/site-packages (from coincurve==5.2.0)
Requirement already satisfied: asn1crypto in /usr/local/lib/python2.7/site-packages (from coincurve==5.2.0)
Requirement already satisfied: pycparser in /usr/local/lib/python2.7/site-packages (from cffi>=1.3.0->coincurve==5.2.0)
Installing collected packages: coincurve, pysha3
Running setup.py install for pysha3 ... done
Successfully installed coincurve-5.2.0 pysha3-1.0.2
Home:Dev m$ pip show coincurve
Name: coincurve
Version: 5.2.0
Summary: Cross-platform Python CFFI bindings for libsecp256k1
Home-page: https://github.com/ofek/coincurve
Author: Ofek Lev
Author-email: ofekmeister@gmail.com
License: MIT/Apache-2.0
Location: /usr/local/lib/python2.7/site-packages
Requires: cffi, asn1crypto
Home:Dev m$ python
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import coincurve
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named coincurve
>>>
Answer
The issue you're encountering arises because the pip
command and the python
command may not be referring to the same Python environment. When you run sudo pip install
, you're installing the packages for the system's default Python environment, which is Python 2.7 in your case. However, when you run the python
command, it may refer to a different Python environment that doesn't have access to the installed packages.
To troubleshoot and resolve this issue, follow these steps:
1. Check Which Python Version Is Used
First, let's confirm that the python
and pip
commands are referring to the same Python version.
-
Run the following commands:
which python which pip
This will show you the paths for
python
andpip
. They should both point to locations within the same Python installation. -
If
pip
points to a different installation (e.g., a user installation like/Users/username/.local/
), it might be installing packages for a different Python version, not the system's Python 2.7.
2. Ensure Correct pip
for Python 2.7
You should make sure you're using pip
associated with Python 2.7, especially when installing packages with sudo
. The easiest way is to use pip2
instead of pip
.
-
Install the packages using
pip2
:sudo pip2 install coincurve==5.2.0 pysha3
-
You can also verify
pip2
points to the correct version:which pip2
3. Check Installed Packages in Python 2.7
After installing the packages, make sure that the Python interpreter is pointing to the correct site-packages
directory:
-
Start Python 2.7 by running:
python
-
Once inside the Python shell, check the module search path (
sys.path
):import sys print(sys.path)
Ensure that
/usr/local/lib/python2.7/site-packages
is included in the list of directories. This is wherecoincurve
andpysha3
should have been installed. -
If it's missing, you may need to adjust your Python environment or installation.
4. Using sudo
Correctly
Sometimes, running sudo
with pip
might cause issues with permissions, as sudo
can cause pip
to install packages globally (and not for your user). You might want to use sudo -H
to avoid this issue, as suggested by the warnings in your terminal:
sudo -H pip install coincurve==5.2.0 pysha3
5. Using Virtual Environments (Optional)
For managing dependencies more easily and avoiding issues with system-wide Python installations, it's a good practice to use virtual environments. This isolates your package installations from the system's Python environment.
-
You can create a virtual environment with
virtualenv
:sudo pip2 install virtualenv virtualenv venv source venv/bin/activate pip install coincurve==5.2.0 pysha3
Then, run your script inside the virtual environment, where the necessary modules will be available.
Conclusion:
- Ensure that
python
andpip
are using the same Python environment (preferably Python 2.7 in your case). - Use
sudo -H pip
to avoid permission issues when installing packages globally. - Consider using a virtual environment for better package management and isolation from the system Python.
Let me know if you encounter any further issues!