如何在linux创建python虚拟环境
2018-02-19 23:01
661 查看
1、大多数系统模块安装在sys.prefix 环境变量指定的目录
tgao@tgao-Latitude-E6410:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> sys.prefix
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> import sys
>>> sys.prefix
'/usr'
2、使用pip或者easy_install安装的第三方模块通常在site packages目录:
import site
site.getsitepackages()
3、
tgao@tgao-Latitude-E6410:~/djangoPractice$ ls
djangoEnv1 env_A
tgao@tgao-Latitude-E6410:~/djangoPractice$ cd env_A
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ ls
bin include lib lib64 pyvenv.cfg share
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ source env/bin/activate
bash: env/bin/activate: No such file or directory
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ source bin/activate
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ ls
bin include lib lib64 pyvenv.cfg share
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pwd
/home/tgao/djangoPractice/env_A
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ deactivate
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ ls
bin include lib lib64 pyvenv.cfg share
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pwd
/home/tgao/djangoPractice/env_A
5、
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ source activate
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ cd ..
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pwd
/home/tgao/djangoPractice/env_A
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip3 install django
Collecting django
Downloading Django-2.0.2-py3-none-any.whl (7.1MB)
100% |████████████████████████████████| 7.1MB 103kB/s
Collecting pytz (from django)
Downloading pytz-2018.3-py2.py3-none-any.whl (509kB)
100% |████████████████████████████████| 512kB 753kB/s
Installing collected packages: pytz, django
Successfully installed django-2.0.2 pytz-2018.3
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip3 install --upgrade pip3
Collecting pip3
Could not find a version that satisfies the requirement pip3 (from versions: )
No matching distribution found for pip3
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip install --upgrade pip
Collecting pip
Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 255kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.1
Uninstalling pip-8.1.1:
Successfully uninstalled pip-8.1.1
Successfully installed pip-9.0.1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip freeze
Django==2.0.2
pkg-resources==0.0.0
pytz==2018.3
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip3 freeze
Django==2.0.2
pkg-resources==0.0.0
pytz==2018.3
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ deactivate
6、开始django第一各项目
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ bin/django-admin startproject pro1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ cd pro1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ ls
manage.py pro1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ pwd
/home/tgao/djangoPractice/env_A/pro1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
February 19, 2018 - 13:24:34
Django version 2.0.2, using settings 'pro1.settings'
Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
[19/Feb/2018 13:25:50] "GET / HTTP/1.1" 200 16348
[19/Feb/2018 13:25:50] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[19/Feb/2018 13:25:51] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
[19/Feb/2018 13:25:51] "GET /static/admin/fonts/Roboto-Bold-webfont
ac39
.woff HTTP/1.1" 200 82564
[19/Feb/2018 13:25:51] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
Not Found: /favicon.ico
[19/Feb/2018 13:25:51] "GET /favicon.ico HTTP/1.1" 404 1970
^C(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ python manage.pyigrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
February 19, 2018 - 13:26:35
Django version 2.0.2, using settings 'pro1.settings'
Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
[19/Feb/2018 13:26:45] "GET / HTTP/1.1" 200 16348
7、查看虚拟环境内外的差别
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ source activate
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ which python3
/home/tgao/djangoPractice/env_A/bin/python3
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ deactivate
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ which python3
/usr/bin/python3
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ echo $PATH
/home/tgao/bin:/home/tgao/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ source activate
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ echo $PATH
/home/tgao/djangoPractice/env_A/bin:/home/tgao/bin:/home/tgao/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/home/tgao/djangoPractice/env_A'
>>>
KeyboardInterrupt
>>> exit()
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ deactivate
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/usr'
>>>
tgao@tgao-Latitude-E6410:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> sys.prefix
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> import sys
>>> sys.prefix
'/usr'
2、使用pip或者easy_install安装的第三方模块通常在site packages目录:
import site
site.getsitepackages()
3、
什么是Python虚拟环境?
Python虚拟环境可以为项目创建相互独立的开发环境,也就是你可以为每个项目安装各自使用依赖模块。4、如何创建虚拟环境tgao@tgao-Latitude-E6410:~/djangoPractice$ pyvenv env_Atgao@tgao-Latitude-E6410:~/djangoPractice$ ls
djangoEnv1 env_A
tgao@tgao-Latitude-E6410:~/djangoPractice$ cd env_A
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ ls
bin include lib lib64 pyvenv.cfg share
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ source env/bin/activate
bash: env/bin/activate: No such file or directory
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ source bin/activate
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ ls
bin include lib lib64 pyvenv.cfg share
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pwd
/home/tgao/djangoPractice/env_A
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ deactivate
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ ls
bin include lib lib64 pyvenv.cfg share
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pwd
/home/tgao/djangoPractice/env_A
5、
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ source activate
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ cd ..
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pwd
/home/tgao/djangoPractice/env_A
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip3 install django
Collecting django
Downloading Django-2.0.2-py3-none-any.whl (7.1MB)
100% |████████████████████████████████| 7.1MB 103kB/s
Collecting pytz (from django)
Downloading pytz-2018.3-py2.py3-none-any.whl (509kB)
100% |████████████████████████████████| 512kB 753kB/s
Installing collected packages: pytz, django
Successfully installed django-2.0.2 pytz-2018.3
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip3 install --upgrade pip3
Collecting pip3
Could not find a version that satisfies the requirement pip3 (from versions: )
No matching distribution found for pip3
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip install --upgrade pip
Collecting pip
Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 255kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.1
Uninstalling pip-8.1.1:
Successfully uninstalled pip-8.1.1
Successfully installed pip-9.0.1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip freeze
Django==2.0.2
pkg-resources==0.0.0
pytz==2018.3
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ pip3 freeze
Django==2.0.2
pkg-resources==0.0.0
pytz==2018.3
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ deactivate
6、开始django第一各项目
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ bin/django-admin startproject pro1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A$ cd pro1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ ls
manage.py pro1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ pwd
/home/tgao/djangoPractice/env_A/pro1
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
February 19, 2018 - 13:24:34
Django version 2.0.2, using settings 'pro1.settings'
Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
[19/Feb/2018 13:25:50] "GET / HTTP/1.1" 200 16348
[19/Feb/2018 13:25:50] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[19/Feb/2018 13:25:51] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
[19/Feb/2018 13:25:51] "GET /static/admin/fonts/Roboto-Bold-webfont
ac39
.woff HTTP/1.1" 200 82564
[19/Feb/2018 13:25:51] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
Not Found: /favicon.ico
[19/Feb/2018 13:25:51] "GET /favicon.ico HTTP/1.1" 404 1970
^C(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ python manage.pyigrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/pro1$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
February 19, 2018 - 13:26:35
Django version 2.0.2, using settings 'pro1.settings'
Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
[19/Feb/2018 13:26:45] "GET / HTTP/1.1" 200 16348
7、查看虚拟环境内外的差别
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ source activate
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ which python3
/home/tgao/djangoPractice/env_A/bin/python3
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ deactivate
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ which python3
/usr/bin/python3
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ echo $PATH
/home/tgao/bin:/home/tgao/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ source activate
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ echo $PATH
/home/tgao/djangoPractice/env_A/bin:/home/tgao/bin:/home/tgao/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/home/tgao/djangoPractice/env_A'
>>>
KeyboardInterrupt
>>> exit()
(env_A) tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ deactivate
tgao@tgao-Latitude-E6410:~/djangoPractice/env_A/bin$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/usr'
>>>
相关文章推荐
- linux 下创建虚拟环境 python
- Linux系统 conda 创建python虚拟环境
- Linux 创建python虚拟环境
- Python学习——如何创建虚拟环境Virtualenv
- Python linux 上的管理工具 pyenv 安装, pip 使用, python项目(版本分割, 项目分割, 虚拟环境创建)
- Winsows下和Linux下创建python虚拟环境virtualenv
- windows Linux Mac 使用virtualenv virtulenvwrapper创建 多个python虚拟环境
- linux下使用virtualenv虚拟独立python环境
- 如何在Linux桌面环境中使用DeVeDe工具创建视频DVD
- 使用virtualenv创建虚拟python环境
- python和flask如何在linux上创建应用
- 如何在Linux中用命令行工具管理KVM虚拟环境
- ubuntu14中创建python虚拟环境
- windows和linux中搭建python集成开发环境IDE——如何设置多个python环境
- 使用virtualenv创建虚拟python环境
- 如何在virtualenv虚拟环境中安装mysql-python
- 如何在Linux中用命令行工具管理KVM虚拟环境
- python 在linux环境下如何安装
- 使用virtualenv创建虚拟python环境
- 创建Python虚拟环境---Virtualenv