virtualenv is a tool to create isolated Python environments.
virtualenv 是一个创建单独的 Python 环境的工具。
The basic problem being addressed is one of dependencies and versions, and indirectly permissions.
要解决的基本问题是依赖关系和版本,以及间接权限。
virtualenv creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).
virtualenv 创建一个具有自己的安装目录的环境,该环境不与其他 virtualenv 环境共享库 (并且可选地也不访问全局安装的库)。
1.1. Installation
To install globally with pip (if you have pip 1.3 or greater installed globally):
$ [sudo] pip install virtualenv
strong@foreverstrong:~$ sudo pip install virtualenv
[sudo] password for strong:
The directory '/home/strong/.cache/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 '/home/strong/.cache/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 virtualenv
Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 732kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
Or to get the latest unreleased dev version:
$ [sudo] pip install https://github.com/pypa/virtualenv/tarball/develop
To install version X.X globally from source:
$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ [sudo] python setup.py install
To use locally from source:
$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ python virtualenv.py myVE
2. 创建一个包含该虚拟环境的目录,以及将来可能会创建的任意虚拟环境。
strong@foreverstrong:~$ mkdir virtualenv
3. 创建虚拟环境,使位于 /home/strong/virtualenv/tensorflow 下。
strong@foreverstrong:~$ virtualenv --system-site-packages /home/strong/virtualenv/tensorflow
New python executable in /home/strong/virtualenv/tensorflow/bin/python
Installing setuptools, pip, wheel...done.
4. 激活该虚拟环境 (激活后命令提示窗口变为 (tensorflow)$)。
strong@foreverstrong:~$ source ~/virtualenv/tensorflow/bin/activate
(tensorflow) strong@foreverstrong:~$
5. 虚拟环境不使用时用以下命令关闭。
(tensorflow) strong@foreverstrong:~$ deactivate
strong@foreverstrong:~$
6. 建立快捷方式。向 ~/.bashrc 文件添加一个别名,使在需要启动虚拟环境时只需键入 tensorflow。
strong@foreverstrong:~$ sudo printf '\nalias tensorflow="source ~/virtualenv/tensorflow/bin/activate"' >> ~/.bashrc
7. 重新启动 terminal,键入 tensorflow,测试。
strong@foreverstrong:~$ tensorflow
(tensorflow) strong@foreverstrong:~$ deactivate
2.1. Python 2.7 - tensorflow-0.9.0-cp27-none-linux_x86_64.whl
strong@foreverstrong:~$ tensorflow
(tensorflow) strong@foreverstrong:~$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
Collecting tensorflow==0.9.0 from https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
Downloading https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl (27.6MB)
100% |████████████████████████████████| 27.6MB 76kB/s
Requirement already up-to-date: numpy>=1.8.2 in ./.local/lib/python2.7/site-packages (from tensorflow==0.9.0)
Requirement already up-to-date: six>=1.10.0 in ./.local/lib/python2.7/site-packages (from tensorflow==0.9.0)
Collecting protobuf==3.0.0b2 (from tensorflow==0.9.0)
Downloading protobuf-3.0.0b2-py2.py3-none-any.whl (326kB)
100% |████████████████████████████████| 327kB 1.3MB/s
Requirement already up-to-date: wheel in ./virtualenv/tensorflow/lib/python2.7/site-packages (from tensorflow==0.9.0)
Requirement already up-to-date: setuptools in ./virtualenv/tensorflow/lib/python2.7/site-packages (from protobuf==3.0.0b2->tensorflow==0.9.0)
Installing collected packages: protobuf, tensorflow
Found existing installation: protobuf 3.4.0
Not uninstalling protobuf at /usr/local/lib/python2.7/dist-packages, outside environment /home/strong/virtualenv/tensorflow
Successfully installed protobuf-3.0.0b2 tensorflow-0.9.0
(tensorflow) strong@foreverstrong:~$
2.2. Try your first TensorFlow program
strong@foreverstrong:~$ tensorflow
(tensorflow) strong@foreverstrong:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> sess.run(hello)
'Hello, TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> sess.run(a + b)
42
>>> sess.close()
>>> exit()
(tensorflow) strong@foreverstrong:~$ deactivate
strong@foreverstrong:~$