在PythonAnywhere使用Virtualenv时出现错误的Python版本

5
为了使用Python 3.3和Django 1.8,我在PythonAnywhere上使用Virtualenv(用于Web应用程序)。
我按照以下说明进行操作:https://help.pythonanywhere.com/pages/VirtualEnvForNewerDjango 进入控制台后,显示我正在使用版本3.3。
(django18)12:04 ~ $ python
Python 3.3.6 (default, Jan 28 2015, 17:27:09) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

这也是我在我的 Web 应用程序中指定要使用的版本:

Python version:3.3 (in the code pane)

然而,当我在Virtualenv窗格中指向Virtualenv目录(名为django18)时,我会收到以下警告:

This virtualenv seems to have the wrong Python version (2.7 instead of 3.3).

这里是控制台输出的所有内容(我在创建应用并指定 Python 版本后运行了它):

        06:43 ~ $ mkvirtualenv --python=/usr/bin/python3.3 django18
Running virtualenv with interpreter /usr/bin/python3.3
Using base prefix '/usr'
New python executable in django18/bin/python3.3
Not overwriting existing python script django18/bin/python (you must use django18/b
in/python3.3)
Installing setuptools, pip, wheel...done.
(django18)06:44 ~ $ which pip
/home/yschellekens/.virtualenvs/django18/bin/pip
(django18)06:44 ~ $ pip install django
Requirement already satisfied (use --upgrade to upgrade): django in ./.virtualenvs/
django18/lib/python3.3/site-packages
(django18)06:44 ~ $ which django-admin.py
/home/yschellekens/.virtualenvs/django18/bin/django-admin.py
(django18)06:44 ~ $ django-admin.py --version
1.8.3
(django18)06:44 ~ $ django-admin.py startproject mysite
CommandError: '/home/yschellekens/mysite' already exists

同时参见:

08:29 ~/.virtualenvs/django18/bin $ ls
__pycache__       django-admin.py   pip           postdeactivate  python3
activate          django-admin.pyc  pip2          preactivate     python3.3
activate.csh      easy_install      pip2.7        predeactivate   wheel
activate.fish     easy_install-2.7  pip3          python
activate_this.py  easy_install-3.3  pip3.3        python2
django-admin      get_env_details   postactivate  python2.7
08:29 ~/.virtualenvs/django18/bin $

我应该在哪里指向Python 3.3?


你确定它是同一个目录吗? - jonrsharpe
你是如何在终端中开始使用 django18 的?activate 脚本在哪里?通常它们会被放置在 ~/.virtualenvs 中,但并不是必须的。 - jonrsharpe
两个需要检查的事项:
  1. 在创建虚拟环境时,您是否指定了Python版本?尝试删除虚拟环境并重新创建。
  2. 当您在已激活虚拟环境的Bash控制台中运行which python命令时,您看到的路径是什么?
- conrad
谢谢@conrad,看到新的编辑了。 - Yehoshaphat Schellekens
2个回答

6

我看你的virtualenv里面似乎同时有Python 2.7和Python 3.3版本。尝试删除并重新创建:

rmvirtualenv django18
mkvirtualenv --python=/usr/bin/python3.3 django18
pip install django # reinstall django and any other packages you need.

顺便问一下,为什么不使用Python 3.4呢?


谢谢,它起作用了!关于3.4,我想你是对的,这就是我在本地PC上安装的版本,我想我得升级它 :) - Yehoshaphat Schellekens

1
因为你正在使用Python 2.7创建环境,然后又使用Python 3.3,所以django18/bin/python脚本仍然指向Python 2.7。编辑django18/bin/python或删除该环境,并仅使用mkvirtualenv --python=/usr/bin/python3.3 django18命令。
旧答案:
从您提供的链接中:
提示:如果要在虚拟环境中使用Python 3,请使用mkvirtualenv --python=/usr/bin/python3.4 django18。

https://www.pythonanywhere.com/wiki/VirtualEnvForNewerDjango


兄弟,感谢你的帮助,但我已经阅读了这篇文章并按照它的指示操作了,所以把它链接给我也没用 :) - Yehoshaphat Schellekens
1
你没有提供足够的信息,所以我无法知道。请查看编辑。 - Krzusztof
你能否前往 django18/bin/ 目录下,运行 ls -l python* 命令并展示结果? - Krzusztof
嗨@Mitius,我已经根据您的要求编辑了我的问题,谢谢! - Yehoshaphat Schellekens
请使用-l标志。我的包含类似这样的内容:lrwxrwxrwx 1 root root 6 Mar 6 11:05 python2.7 -> python 如果2.7和3.3都指向Python,那么我们就有线索了。 - Krzusztof
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接