Python for mac 中包安装管理——No module named numpy引

作者:编程技术

目录

前言

一 管理虚拟环境软件

  1.1 Mac Python管理虚拟环境软件安装-Pyenv具体的流程

  1.2 Mac Python管理虚拟环境软件安装-Anaconda具体的流程

  1.3 Mac Python管理虚拟环境软件安装-pip具体的流程

二 探索的起因

  2.1 具体的问题(报错)

  2.2 先检查Python的安装与路径

  2.3 安装包——Numpy

  2.4 安装包——matplotlib

  2.5 安装包——scipy

  2.6 安装pandas

  2.7 安装TensorFlow 

 

 

 

其他命令

bash命令
which python
在系统环境变量中定位python的directory
pyenv which python
在shims中定位python的directory

python directory structure

2.2 先检查Python的版本与路径

1 查看python版本

#注意:‘-V‘中‘V’为大写字母,只有一个‘-’
python -V  

 

#注意:‘--version'中有两个‘-’
python --version 

 

2 查看python安装位置

python3以上的版本 注意print的时候使用的是括号,python3以下版本的不需要括号

python -c "import sys; print (sys.executable)"

 

python -c "import os; print (os.sys.executable)" python -c "import os; path = os.sys.executable;folder=path[0 : path.rfind(os.sep)]; print folder"

 

 

 

最近同时看两本书,都是关于如何利用python做数据分析的。
两本书对应两种业务场景,因此分别对应着两个不同的开发环境(其实是对应不同的发行版本)。这就很麻烦了:

Find system's python package directory:

>>> import site; site.getsitepackages()
['/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages', '/usr/lib/site-python']

/usr/lib/python2.7/site-packages seems to be important ?

if I using sudo pip install virtualenv it will install virtualenv package into /usr/bin


二 探索的起因

一个常见的环境冲突例子

默认的python是canopy,因为其他需求,需要安装anaconda。安装后,bash优先调用anaconda,因为python的升级很多时候是不考虑前向兼容的,容易莫名其妙出现一些之前从未出现的报错或警告。

create Python 3 environment

$ conda create -n py35 python=3.5 anaconda
$ source activate py35
$ source deactivate

Here, the ‘py35’ is the name of the environment you want to create, and ‘anaconda’ is the meta-package that includes all of the actual Python packages comprising the Anaconda distribution. When creating a new environment and installing Anaconda, you can specify the exact package and Python versions, for example, numpy=1.7 or python=3.5.

2.3 安装包——Numpy(pip)

1 查看Numpy版本 

 

python -c "import numpy; print (numpy.version.version)"

python3 -c "import numpy; print (numpy.__version__)"

 

2 查看Numpy安装路径 

#python2.x版本
python -c "import numpy; print (numpy.__file__)"

python -c "import numpy; print (numpy.__file__)"

 

 

#python3.x版本
python3 -c "import numpy; print (numpy.__file__)"

python3 -c "import numpy; print (numpy.__file__)"

 

3 安装

$pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose --prefix=~/local

 

4 将package安装到指定目录:通过源码安装一个python包的时候,例如安装xlrd,目标路径为/usr/local/lib/python2.7/site-packages/

$ pip install -t /usr/local/lib/python2.7/site-packages/ xlrd

 

5 或者我已经测试成功的,网站为

$mac os x: Python 3 安装(scipy,numpy,matplotlib. . .)

 

 

 

Homebrew安装pyenv

安装pyenv有多种方式,Homebrew是其中一种。这种方式是不修改系统环境变量的。因此Homebrew不会破坏OS X 原生的环境。同时它安装的所有文件都是在用户独立空间内的,这让你安装的所有软件对于该用户来说都是可以访问的,不需要使用sudo命令。

Homebrew安装pyenv的警告:
1.使用Homebrew的目录:

==> Caveats
To use Homebrew's directories rather than ~/.pyenv add to your profile:
export PYENV_ROOT=/usr/local/var/pyenv

这里的意思在profile里执行,用于bash里初始化环境变量:

echo 'PYENV_ROOT=/usr/local/var/pyenv' >>~/.bash_profile

目前建议不要这样子做,用Homebrew的目录并没有好处,而且容易产生问题。

2.激活shims:

==> Caveats
To enable shims and autocompletion add to your profile:
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

不加这一行的情况下,当我们使用pyenv global 3.5.1后现在指向了3.5.1,但是当运行python version却还显示的是2.7.2

echo 'if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi' >>~/.bash_profile

Python Packaging Ecosystem

  • A package is simply a directory with an init.py file inside it.
  • distutils was created to install packages into the PYTHONPATH with little difficulty. The PYTHONPATH, also sys.path in code, is a list of locations to look for Python packages.
  • if PYTHONPATH is set to /www/python:/opt/py, the search path will begin with ['', '/www/python', '/opt/py', ...].
>>> import sys
>>> sys.path
['',   # the current directory, represented by a null string
 '/usr/local/lib/python2.6',
 '/usr/local/lib/python2.6/site-packages',
 ...]
>>> import mypackage  # this package is at current directory
>>> mypackage.__file__
'mypackage/__init__.py'
  • The convention way of manually installing packages is to put them in the .../site-packages/ directory. But one may want to install Python modules into some arbitrary directory.

  • The most convenient way is to add a path configuration file to a directory that’s already in Python’s path, which could be the .../site-packages/ directory. Path configuration files have an extension of .pth, and each line must contain a single path that will be appended to sys.path.

  • Finally, sys.path is just a regular Python list, so any Python application can modify it by adding or removing entries.

  • Current python packaging tools

    • Distribute
    • pip, which replace easy_install
    • virtualenv
  • Distribute is a collection of enhancements to the Python standard library module: distutils (for Python 2.3.5 and up on most platforms; 64-bit platforms require a minimum of Python 2.4) that allows you to more easily build and distribute Python packages, especially ones that have dependencies on other packages. Distribute was created because the Setuptools package is no longer maintained.


前言

大家要在开发中,使用多个python版本的,强烈建议使用安装管理器工具管理虚拟环境,不然就会出现以下一系列报错。因此会教你如何安装Python的 pandas等各类包。

安装brew,安装教程。我们通过brew可以快捷方便的下载我们需要的各类软件,包括pyenv,Anaconda,virtualenv(虚拟环境)等。我们通过以下途径来完成python包的管理:

 

1. Anaconda:安装pandas、Python和SciPy最简单的方式是用Anaconda。Anaconda是关于Python数据分析和科学计算的分发包。

  1. Miniconda
    使用Anaconda会安装一百多个依赖包,如果想灵活控制安装的依赖包或带宽有限,使用Miniconda是个不错的选择。
    Conda是个包管理器,Anaconda就是建立在它的基础上。Conda不只跨平台还与语言无关,与pip和virtualenv相结合的作用相似。
    Miniconda允许先创建包含Python的安装包,然后用conda安装其他的依赖包。

 

  1. pip
    pandas可以通过pip安装,但要安装相关的依赖包。
    [plain] view plain copy
    pip install pandas

 

  1. 包管理器
    可以用linux的包管理器进行安装,如
    [plain] view plain copy
    sudo apt-get install python-pandas 
    zypper in python-pandas

 

  1. 源码安装
    从源码安装需要安装最新的Cython,可用easy-install -U cython安装。源码位于

[plain] view plain copy
git clone git://github.com/pydata/pandas.git 
cd pandas 
python setup.py install

 

 

 

Reference

homebrew是ruby写的OS X 下的包管理工具
Python多版本切换工具-Pyenvvirtualenv及Anaconda科学计算环境的配置
Python之包管理工具
用了pyenv-virtualenv, 天黑都不怕
Python系列(2)之pyenv进行多版本管理
一篇文章入门Python生态系统
Python Virtual Environments - a Primer
#!/usr/bin/env 脚本解释程序的作用
PEP 405 -- Python Virtual Environments
virtualenv
Openstack工程的持续集成实践1--tox

Linux

2.1 具体的问题(报错)

半路出家,调试代码中出现以下错误

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ModuleNotFoundError: No module named 'numpy'

 

Traceback (most recent call last):

  File "MLCNN.py", line 8, in <module>

    import matplotlib.pyplot as plt

ModuleNotFoundError: No module named 'matplotlib'

 

Traceback (most recent call last):

  File "MLCNN.py", line 9, in <module>

    import scipy.io

ModuleNotFoundError: No module named 'scipy'

 

Traceback (most recent call last):

  File "MLCNN.py", line 11, in <module>

    import tensorflow as tf

ModuleNotFoundError: No module named 'tensorflow'

 

Traceback (most recent call last):
File "MLCNN.py", line 12, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'

 

 

 

 

其他

Source Distributions vs Wheels

  • pip can install from either Source Distributions (sdist) or Wheels, but if both are present on PyPI, pip will prefer a compatible wheel.
  • Wheels are a pre-built distribution format that provides faster installation compared to Source Distributions (sdist), especially when a project contains compiled extensions.

解决方案2——用Anaconda来进行激活使用(未解决)

首先安装Anaconda,安装成功后,创建一个conda环境

 

conda create -n tensorflow pip python = 2.7
conda create -n tensorflow pip python = 3.6
#或python = 3.3等

 

激活环境

 

source activate tensorflow
# (targetDirectory)$   Your prompt should change

 

在环境中安装Tensorflow

 

pip install --ignore-installed --upgrade   https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.6.0-py2-none-any.whl

#TensorFlow for Python 2.7的纯CPU版本

 

pip install --ignore-installed --upgrade   https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.6.0-py3-none-any.whl

#Python 3.4,3.5或3.6

 

 

参考:

   -tensorflow官网

 

 

 

正确的安装方法

⭐️   -csdn/mac/linux 安装tensorflow和安装Anaconda

 

tensorflow拓展学习

   -tensorflownews/tensorflownews/个人网站

 

 

 

参考:

✅   -cnblogs/在 Mac OS X 上安装 TensorFlow

 

 

 

 

参考:

   -Mac下如何安装配置Homebrew

   -Linux/numpy的下载与安装教程——(解决No module named numpy问题)

   -python和numpy的版本、安装位置

   -安装Numpy

   -Mac/MAC 使用pycharm出现ImportError: No module named numpy 解决方法

   -安装python package到指定目录

   -多版本Python共存时pip给指定版本的python安装package的方法

   -[Mac] Python Numpy Scipy Matplotlib 快速安装

⭐️   -mac os x: Python 3 安装(scipy,numpy,matplotlib. . .)

   -ImportError: No module named matplotlib

   - tensorflow

 

 

2018-03-0809:00:00

2018-03-0821:17:12

 

pyenv-virtualenv插件管理虚拟环境

pyenv不是管理虚拟环境的工具,但是有一个叫做 pyenv-virtualenv 的插件可以自动化不同环境的创建,而且也能够使用现有的pyenv工具,基于环境变量或者 .python-version文件,来切换不同的环境。(和virtualenv的作用一样)

Install Package

https://packaging.python.org/installing/

It’s important to note that the term “package” in this context is being used as a synonym for a distribution (i.e. a bundle of software to be installed), not to refer to the kind of package that you import in your Python source code (i.e. a container of modules). It is common in the Python community to refer to a distribution using the term “package”. Using the term “distribution” is often not preferred, because it can easily be confused with a Linux distribution, or another larger software distribution like Python itself.


1.1 Mac Python管理虚拟环境软件安装-Pyenv具体的流程

1 先安装管理软件pyenv

个人安装信息

 

87:~ jss$ brew install pyenv

Updating Homebrew...

==> Auto-updated Homebrew!

Updated 1 tap (homebrew/core).

==> Updated Formulae

app-engine-java     geth                lorem class="Apple-converted-space">              radare2

bit                 getmail             mapnik class="Apple-converted-space">              roswell

calabash            gtk                 node class="Apple-converted-space">                s-nail

cayley              gutenberg           node@4 class="Apple-converted-space">              sassc

conan               gxml                node@6 class="Apple-converted-space">              saxon

diffuse             heroku              node@8 class="Apple-converted-space">              spigot

django-completion class="Apple-converted-space">  igv class="Apple-converted-space">                nspr class="Apple-converted-space">                syncthing

docfx               jbake               odpi class="Apple-converted-space">                tile38

flow                jenkins             onetime             yaml-cpp

fluent-bit          just                openimageio         yarn

flyway              kerl                php

fn                  libsass             plank

 

==> Installing dependencies for pyenv: autoconf**, class="s3">pkg-config**, class="s3">openssl**, class="s3">readline**

==> Installing pyenv dependency: autoconf

==> Downloading

class="s1">######################################################################## 100.0%

==> Pouring autoconf-2.69.high_sierra.bottle.4.tar.gz

==> Caveats

Emacs Lisp files have been installed to:

  /usr/local/share/emacs/site-lisp/autoconf

==> Summary

本文由分分快三计划发布,转载请注明来源

关键词: 分分快三计划 python 我的Pytho... 数据科学&hacker