【问题标题】:Wrong Python version when using Virtualenv in PythonAnywhere在 PythonAnywhere 中使用 Virtualenv 时 Python 版本错误
【发布时间】:2015-10-01 17:12:19
【问题描述】:

为了使用 Python 3.3 和 Django 1.8,我正在使用 Virtualenv(用于 PythonAnywhere 中的网络应用)

我按照以下说明进行操作: 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.
>>>

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

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?

【问题讨论】:

  • 你确定是同一个目录吗?
  • 好吧,你是怎么开始在终端中使用django18的; activate 脚本在哪里?它们通常放置在~/.virtualenvs,但不是必需
  • 需要检查的两件事 1. 创建 virtualenv 时是否指定了 python 版本?尝试删除 virtualenv 并再次创建它? 2. 当您在激活 virtualenv 的 bash 控制台中运行 which python 时,您看到的路径是什么
  • 感谢@conrad 查看新编辑

标签: python django pythonanywhere


【解决方案1】:

在我看来,您的 virtualenv 以某种方式同时包含了 Python 的 2.7 版和 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 上安装的,我想我必须升级它:)
【解决方案2】:

不覆盖现有的 python 脚本 django18/bin/python

因为您使用 python 2.7 创建环境,然后使用 python 3.3,django18/bin/python 脚本仍然指向 python 2.7。编辑django18/bin/python 或删除环境并仅使用mkvirtualenv --python=/usr/bin/python3.3 django18 命令。

旧答案:

从您提供的链接:

提示:如果您想为您的 virtualenv 使用 Python 3,请使用 mkvirtualenv --python=/usr/bin/python3.4 django18

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

【讨论】:

  • 老兄,感谢您的帮助,但我阅读了帖子并关注了它,所以将它链接回我不会有帮助:)
  • 您没有提供足够的信息,所以我无法知道。查看编辑。
  • 谢谢,这样做了,看看我的编辑,还是不行
  • 你能去django18/bin/,运行ls -l python*并显示结果吗?
  • 嗨@Mitius 刚刚根据您的要求编辑了我的问题,谢谢!
猜你喜欢
  • 2020-07-22
  • 2022-06-10
  • 1970-01-01
  • 2023-03-12
  • 2018-10-14
  • 1970-01-01
  • 2010-12-04
相关资源
最近更新 更多