Creating a virtualenv with python 2

时间:2017-10-12 10:23:09

标签: virtualenv

In ubuntu 16.04 I have in bashrc:

alias python=python3.6

In other words, the command python starts python 3.6.

Now I want a virtualenv with python 2.7.

I have installed virtualenv:

virtualenv -p python2 pgadmin4

Then activated it.

Now check:

$ which python
/home/michael/PycharmProjects/venv/pgadmin4/bin/python

Doublecheck:

(pgadmin4) michael@michael-desktop:~/PycharmProjects/venv/pgadmin4/bin$ ls -la
    total 3516
    drwxrwxr-x 2 michael michael    4096 окт 12 13:05 .
    drwxrwxr-x 7 michael michael    4096 окт 12 13:05 ..
    -rw-rw-r-- 1 michael michael    2102 окт 12 13:05 activate
    -rw-rw-r-- 1 michael michael    1044 окт 12 13:05 activate.csh
    -rw-rw-r-- 1 michael michael    2242 окт 12 13:05 activate.fish
    -rw-rw-r-- 1 michael michael    1137 окт 12 13:05 activate_this.py
    -rwxrwxr-x 1 michael michael     272 окт 12 13:05 easy_install
    -rwxrwxr-x 1 michael michael     272 окт 12 13:05 easy_install-2.7
    -rwxrwxr-x 1 michael michael     244 окт 12 13:05 pip
    -rwxrwxr-x 1 michael michael     244 окт 12 13:05 pip2
    -rwxrwxr-x 1 michael michael     244 окт 12 13:05 pip2.7
    lrwxrwxrwx 1 michael michael       7 окт 12 13:05 python -> python2
    -rwxrwxr-x 1 michael michael 3546104 окт 12 13:05 python2
    lrwxrwxrwx 1 michael michael       7 окт 12 13:05 python2.7 -> python2
    -rwxrwxr-x 1 michael michael    2361 окт 12 13:05 python-config
    -rwxrwxr-x 1 michael michael     251 окт 12 13:05 wheel

Well, just writing python must run python2.

But:

$ python
Python 3.6.2 (default, Jul 20 2017, 08:43:29) 

Could you give me a kick here?

1 个答案:

答案 0 :(得分:1)

当您激活virtualenv时,会重写PATH环境变量,以便为您的virtualenv的bin目录提供更多优先级。

which python命令会告诉您所期望的路径,因为忽略最终配置的alias。它查看PATH环境变量并尝试解析当前的python路径。

相反,Bash会考虑所有已配置的alias,当您键入python时,bash会将命令解析为不在您的virtualenv bin目录中的python3.6并转到系统一。

唯一可行的解​​决方案是:

  1. 删除别名
  2. 在virtualenv的python3.6 -> python文件夹中添加符号链接bin