天天看点

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

1.创建Linux虚拟环境的两种方式 

实验前提:在linux中 python3.7 已经安装好了

方式1:venv工具

在新版本python中,venv是自带的

(1).创建目录

[[email protected] ~]# mkdir -p /app/test/
           

(2).创建虚拟环境

[[email protected] ~]# cd /app/test/
# 创建虚拟环境
[[email protected] test]# python3.7 -m venv env

# env为路径, 具体来说就是/app/test/路径下的env目录
[[email protected] test]# ll /app/test/
total 0
drwxr-xr-x. 5 root root 74 May  6 15:51 env
# 并且会在/app/test/env/路径下生成一系列的目录文件
[[email protected] test]# ll /app/test/env/
total 4
drwxr-xr-x. 2 root root 173 May  6 15:51 bin
drwxr-xr-x. 2 root root   6 May  6 15:51 include
drwxr-xr-x. 3 root root  23 May  6 15:51 lib
lrwxrwxrwx. 1 root root   3 May  6 15:51 lib64 -> lib
-rw-r--r--. 1 root root  69 May  6 15:51 pyvenv.cfg
           

(3).激活虚拟环境

[[email protected] test]# . env/bin/activate
           

(4).退出虚拟环境

(test) [[email protected] test]# deactivate
           

(5).进入虚拟环境

[[email protected] test]# pwd
/app/test
# 以下两种方式均可进入虚拟环境
[[email protected] test]# . bin/activate
[[email protected] test]# source bin/activate
           

方式2:virtualenvwrapper 工具

(1).安装virtualenvwrapper

[[email protected] ~]# pip install virtualenvwrapper
           

(2).设置环境变量

[[email protected] ~]# vim .bash_profile 
###################
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
source /usr/bin/virtualenvwrapper.sh
           
创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

(3).刷新配置

[[email protected] ~]# source .bash_profile 
           

(4).创建目录

[[email protected] ~]# mkdir -p /app/test
           

(5).创建虚拟环境

[[email protected] ~]# mkvirtualenv -a /app/test -p python3.7 test
(test) [[email protected] test]# ls
           

(6).退出虚拟环境

(test) [[email protected] test]#  deactivate
           

(7).进入虚拟环境

[r[email protected] test]# cd /app/
[[email protected] app]# workon test
           

(8).删除虚拟环境

# 删除之前需要先退出虚拟环境
(test) [[email protected] test]# deactivate
# 删除虚拟环境
[[email protected] test]# rmvirtualenv test
Removing test...
# 测试发现确实被删除了
[[email protected] test]# cd /app/
[[email protected] app]# workon test
ERROR: Environment 'test' does not exist. Create it with 'mkvirtualenv test'.
           

2. Pycharm 如何连接 Linux虚拟环境

(1).查看虚拟环境路径与python路径

(test) [[email protected] test]# pwd
/app/test
(test) [[email protected] test]# ll /app/test/bin/python3.7 
lrwxrwxrwx. 1 root root 24 May  6 15:00 /app/test/bin/python3.7 -> /usr/local/bin/python3.7
           
创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

(2).新建项目

Location:本地 windows 存储项目路径

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

(3).配置 远程连接

Host:主机IP

Username:用户名

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

Password:主机密码

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

Interpreter:远程 python路径(linux)

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境
创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

(4).配置远程项目路径

Remote project location:远程项目路径(linux)

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

(5).测试

1.新建一个python文件

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

2.可在Linux虚拟环境中查看到

(env) [[email protected] test]# pwd
/app/test
(env) [[email protected] test]# ls
env  file.py
           
创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

如果你是从GitLab上下载到本地的项目,此时项目已经创建好了,要想远程连接Linux虚拟环境,可以进行如下操作

点击 File --- Settings --- Project : ...  --- Project Interpreter 

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

点击 Add - SSH Interpreter,输入主机IP 与 用户名

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

输入 主机密码

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

配置 远程python路径

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

Sync folders:本地 项目路径  --->  远程 项目路径

如果远程连接已经设置好并且已有同步的python文件,但是此时想要修改 本地项目路径名称 该如何操作呢?

本地项目路径:D:\Users\01397470\PycharmProjects\Budget(希望将 Budget 更改为 BudgetManager)

远程项目路径:/app/worksapce/rop_test/

(1).修改项目名

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

(2).修改配置(Local Path)

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

发现问题:修改项目名称后,整个rop_test目录都被删除了

(rop_test) [[email protected] worksapce]# pwd
/app/worksapce
(rop_test) [[email protected] worksapce]# ll
total 0
           

(3).重新 启动 pycharm项目

(4).重新 手动同步 python文件

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

测试:

(rop_test) [[email protected] worksapce]# ll
total 0
drwxr-xr-x. 3 root root 245 Mar 24 10:40 rop_test
(rop_test) [[email protected] worksapce]# cd rop_test/
(rop_test) [[email protected] rop_test]# ll
total 80
-rw-r--r--. 1 root root 31723 Mar 24 10:24 adds.py
-rw-r--r--. 1 root root 13920 Mar 23 11:30 mysql_engine.py
drwxr-xr-x. 2 root root    62 Mar 24 10:40 templates
-rw-r--r--. 1 root root   279 Mar 23 15:58 test.py
-rw-r--r--. 1 root root   363 Mar 17 10:30 test.txt
-rw-r--r--. 1 root root  1661 Mar 17 14:14 上传文件-前后端不分离.py
-rw-r--r--. 1 root root  1646 Mar 17 14:09 上传文件-简单案例.py
-rw-r--r--. 1 root root   498 Mar 19 12:08 合并字典.py
-rw-r--r--. 1 root root  4300 Mar 23 20:04 数据模型备选.py
-rw-r--r--. 1 root root  3036 Mar 17 13:47 文件操作.py
           

有的时候,你会发现在settings里面配置了远程连接以后,并没有生效?

此时你需要检查一下,这个 自动同步 有没有勾选

创建Linux虚拟环境的两种方式 venv 与 virtualenvwrapper 以及 Pycharm 如何连接 Linux虚拟环境

如果已经勾选,但是依旧无法同步,则需要检查 configuration配置里面的路径是否正确