导入在 Python 中有效,但在 IPython 中无效

Import works in Python but not IPython

当我在 python 终端中导入时没有问题:

$ python
Python 3.5.4 | packaged by conda-forge | (default, Aug 10 2017, 01:41:15) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import georasters
>>> from osgeo import gdal

ModuleNotFoundError 当我在 ipython 终端做同样的事情时:

$ ipython
Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09) 
Type "copyright", "credits" or "license" for more information.

IPython 5.3.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import georasters
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-5a74495660ba> in <module>()
----> 1 import georasters

ModuleNotFoundError: No module named 'georasters'

In [2]: from osgeo import gdal
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-a04b80bf4e65> in <module>()
----> 1 from osgeo import gdal

ModuleNotFoundError: No module named 'osgeo'

为什么以及如何解决这个问题?我有 MAC El Capitan 和 python 3.6.1。我已经用 conda install ... 安装了所有东西,谢谢。


conda list

的输出
$ conda list
# packages in environment at /Users/<myname>/anaconda/envs/cv:
#
affine                    2.1.0                    py35_0    conda-forge
attrs                     17.2.0                   py35_0    conda-forge
boost                     1.64.0                   py35_3    conda-forge
boost-cpp                 1.64.0                        1    conda-forge
boto3                     1.4.4                    py35_0    conda-forge
botocore                  1.5.92                   py35_0    conda-forge
bzip2                     1.0.6                         1    conda-forge
ca-certificates           2017.7.27.1                   0    conda-forge
cairo                     1.14.6                        4    conda-forge
cartopy                   0.15.1              np112py35_2    conda-forge
certifi                   2017.7.27.1              py35_0    conda-forge
chardet                   3.0.2                    py35_1    conda-forge
click                     6.7                      py35_0    conda-forge
click-plugins             1.0.3                    py35_0    conda-forge
cligj                     0.4.0                    py35_0    conda-forge
cloudpickle               0.3.1                    py35_0    conda-forge
curl                      7.54.1                        0    conda-forge
cycler                    0.10.0                   py35_0    conda-forge
dask                      0.13.0                   py35_0    ozak
decorator                 4.1.2                    py35_0    conda-forge
descartes                 1.1.0                    py35_0    conda-forge
docopt                    0.6.2                    py35_0    conda-forge
docutils                  0.13.1                   py35_0    conda-forge
expat                     2.1.0                         3    conda-forge
fiona                     1.7.8                    py35_1    conda-forge
fontconfig                2.12.1                        4    conda-forge
freetype                  2.7                           1    conda-forge
freexl                    1.0.2                         2    conda-forge
gdal                      2.2.0               np112py35_2    conda-forge
geopandas                 0.2.1                    py35_4    conda-forge
geopy                     1.11.0                   py35_0    conda-forge
georasters                0.5.6                    py35_0    ozak
geos                      3.5.1                         1    conda-forge
gettext                   0.19.8.1                      0    conda-forge
giflib                    5.1.4                         0    conda-forge
glib                      2.51.4                        0    conda-forge
hdf4                      4.2.12                        0    conda-forge
hdf5                      1.8.18                        1    conda-forge
hypothesis                3.17.0                   py35_0    conda-forge
icu                       58.1                          1    conda-forge
idna                      2.5                      py35_0    conda-forge
imageio                   2.2.0                    py35_0    conda-forge
jmespath                  0.9.3                    py35_0    conda-forge
jpeg                      9b                            0    conda-forge
json-c                    0.12.1                        0    conda-forge
kealib                    1.4.7                         2    conda-forge
krb5                      1.14.2                        0    conda-forge
libdap4                   3.18.3                        2    conda-forge
libffi                    3.2.1                         3    conda-forge
libgdal                   2.1.4                         2    conda-forge
libgfortran               3.0.0                         0    conda-forge
libiconv                  1.14                          4    conda-forge
libkml                    1.3.0                         1    conda-forge
libnetcdf                 4.4.1.1                       6    conda-forge
libpng                    1.6.28                        1    conda-forge
libpq                     9.6.3                         0    conda-forge
libspatialindex           1.8.5                         1    conda-forge
libspatialite             4.3.0a                       15    conda-forge
libssh2                   1.8.0                         1    conda-forge
libtiff                   4.0.6                         7    conda-forge
libxml2                   2.9.4                         4    conda-forge
libxslt                   1.1.29                        5    conda-forge
lxml                      3.8.0                    py35_0    conda-forge
matplotlib                2.0.2                    py35_2    conda-forge
mkl                       2017.0.3                      0  
munch                     2.2.0                    py35_0    conda-forge
ncurses                   5.9                          10    conda-forge
networkx                  1.11                     py35_0    conda-forge
numpy                     1.12.1                   py35_0  
olefile                   0.44                     py35_0    conda-forge
openjpeg                  2.1.2                         2    conda-forge
openssl                   1.0.2l                        0    conda-forge
owslib                    0.14.0                   py35_0    conda-forge
pandas                    0.20.3                   py35_1    conda-forge
pcre                      8.39                          0    conda-forge
pillow                    4.2.1                    py35_1    conda-forge
pip                       9.0.1                    py35_0    conda-forge
pixman                    0.34.0                        0    conda-forge
poppler                   0.52.0                        2    conda-forge
poppler-data              0.4.7                         0    conda-forge
proj4                     4.9.3                         4    conda-forge
psycopg2                  2.7.3                    py35_0    conda-forge
pyepsg                    0.3.2                    py35_0    conda-forge
pympler                   0.5                      py35_0    conda-forge
pyparsing                 2.2.0                    py35_0    conda-forge
pyproj                    1.9.5.1                  py35_0    conda-forge
pysal                     1.13.0                   py35_0    conda-forge
pyshp                     1.2.11                   py35_0    conda-forge
python                    3.5.4                         0    conda-forge
python-dateutil           2.6.1                    py35_0    conda-forge
pytz                      2017.2                   py35_0    conda-forge
pywavelets                0.5.2               np112py35_0    conda-forge
rasterio                  0.36.0                   py35_0    conda-forge
rasterstats               0.11.0                   py35_0    ozak
readline                  6.2                           0    conda-forge
requests                  2.18.3                   py35_0    conda-forge
rtree                     0.8.3                    py35_0    conda-forge
s3transfer                0.1.10                   py35_1    conda-forge
scikit-image              0.13.0                   py35_1    conda-forge
scipy                     0.19.1              np112py35_0  
setuptools                36.2.2                   py35_0    conda-forge
shapely                   1.5.17                   py35_4    conda-forge
simplejson                3.11.1                   py35_0    conda-forge
six                       1.10.0                   py35_1    conda-forge
snuggs                    1.4.1                    py35_0    conda-forge
sqlalchemy                1.1.11                   py35_0    conda-forge
sqlite                    3.13.0                        1    conda-forge
tk                        8.5.19                        2    conda-forge
toolz                     0.8.2                    py35_0    conda-forge
tornado                   4.5.1                    py35_0    conda-forge
urllib3                   1.21.1                   py35_0    conda-forge
wheel                     0.29.0                   py35_0    conda-forge
xerces-c                  3.1.4                         3    conda-forge
xz                        5.2.2                         0    conda-forge
zlib                      1.2.8                         3    conda-forge
zope                      1.0                      py35_0  
zope.interface            4.4.2                    py35_0  

看起来你 运行 是 Python 的不同版本(shell 中的 3.5.4 和 IPython 中的 3.6.1)。

您可以使用conda list 列出活动环境中安装的包和版本。我怀疑您是来自不同环境的 运行 和 ipython。查看它是否安装在活动环境中。如果您没有在列表中看到它,请使用 conda install -n {env_name} ipython.

安装它

更新:安装 ipythonconda 后,我总是需要再次 source activate {env_name}。安装其他软件包似乎不需要重新采购。

在我的例子中,正如 tdube 的回答,问题是 Python 的不同版本(shell 中的 2.7.15 和 IPython 中的 3.6.7)。我使用 pip 安装了我的包,默认安装了 python (2.7):

pip install umap-learn      # Not accessible in my IPython

使用 pip3 将软件包安装在 IPython 可访问的位置,该位置位于 Python 3.

pip3 install umap-learn     # Accessible in my IPython