简体   繁体   中英

Astropy isn't working with Python

I've been having difficulty with Python recently, mainly since I think I had several versions and conflicts (due to Anaconda installs, canopy installs etc.). So I cleaned those out.

I reinstalled python (2.7) via brew.

I reinstalled numpy and matplotlib via pip. I also reinstalled astropy and h5py via pip.

However, I get a clean import of numpy and matplotlib, but not of astropy and h5py:

~ > python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import matplotlib
>>> import astropy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named astropy
>>> import h5py
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ImportError: No module named h5py
>>> 

My suspicion is that your pip executable is not linked to your python executable, which means when you run pip install astropy it is installing it in the site-packages for a different python.

One way to make sure you're using the correct pip is to not use

$ pip install astropy

but instead use

$ python -m pip install astropy

If this fails, it probably means that you don't have pip installed for the python instance you're using, and you need to install it (note that for Python 2 version 2.7.9 or later, or Python 3 version 3.4 or later, pip comes bundled with Python).

If this still doesn't work, then something stranger is going on. It may be due to having $PYTHONPATH or $LD_LIBRARY_PATH / $DYLD_LIBRARY_PATH set in a way that interferes with your python imports. In this case, you could clear these variables and try again. Otherwise, I'd consider using a package bundle such as conda or canopy . It makes these kinds of installation issues much smoother.


Edit: I see now that you've used conda and canopy, and you suspect these were causing your problems. Conda and canopy, by design, both sandbox their python installations so that they shouldn't get interference from other installs in your system, unless you force such interference by setting the environment variables I mentioned above. I'd suggest reinstalling conda and wiping those environment variables from your bash/csh startup script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM