您的位置:首页 > 运维架构 > Linux

如何在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、

什么是Python虚拟环境?

Python虚拟环境可以为项目创建相互独立的开发环境,也就是你可以为每个项目安装各自使用依赖模块。4、如何创建虚拟环境tgao@tgao-Latitude-E6410:~/djangoPractice$ pyvenv env_A
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'
>>> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux