简体   繁体   English

Django-admin在virtualenv中创建了错误的Django版本

[英]Django-admin creates wrong django version inside virtualenv

I've created an new directory, a virtualenv and installed a django-toolbelt inside it. 我创建了一个新目录,一个virtualenv,并在其中安装了django-toolbelt。 The django-version should be 1.8 but when I call 'django-admin.py version' it says 1.6. django版本应该是1.8,但是当我叫“ django-admin.py版本”时,它说是1.6。 So when I start a new project it creates a 1.6. 因此,当我开始一个新项目时,它将创建一个1.6。 I thought virtualenv was supposed to prevent this. 我以为virtualenv应该可以防止这种情况。 What am I doing wrong? 我究竟做错了什么?

Edit: I think it has to do with the PATH (?). 编辑:我认为这与PATH(?)有关。 Like it's calling the wrong django-admin version. 就像调用了错误的django-admin版本一样。 I'm on Windows 7. Still don't know how to fix it. 我在Windows 7上。仍然不知道如何修复它。

I came across this problem too. 我也遇到了这个问题。 In the official document, I found that, in a virtual environment, if you use the command 'django-admin' , it would search from PATH usually in '/usr/local/bin' (Linux) to find 'django-admin.py' which is a symlink to another version of django. 在官方文档中,我发现,在虚拟环境中,如果使用命令'django-admin' ,则通常会在PATH中的'/usr/local/bin' (Linux)中从PATH搜索以找到“ django-admin”。 py”,这是django的另一个版本的符号链接。 This is the reason of what happened finally. 这就是最后发生的原因。

So there are two methods to solve this problem: 因此,有两种方法可以解决此问题:

  1. re-symlink your current version django-admin(site-packages/django/bin/django-admin.py) to 'usr/local/bin/django-admin' or 'usr/local/bin/django-admin.py' 重新符号链接您当前的版本django-admin(site-packages / django / bin / django-admin.py)到'usr / local / bin / django-admin'或'usr / local / bin / django-admin.py'
    REMIND : This is a kind of global way so that it will effects your other django projects, so I recommend the second method REMIND :这是一种全局方式,它将影响您的其他django项目,因此我建议第二种方法
  2. cd to your_virtual_env/lib/python3.x/site-packages/django/bin/(of course you should activate your virtual environment), and then use 'python django-admin.py startproject project_name project_full_path' to create django project cd到your_virtual_env / lib / python3.x / site-packages / django / bin /(当然,您应该激活虚拟环境),然后使用'python django-admin.py startproject project_name project_full_path'创建django项目

Try and install Django into the virtual environment as well: 也尝试将Django安装到虚拟环境中:

pip install django

It should install the latest version, you can also specify a particular version (let's say 1.8.2) if you need to: 它应该安装最新版本,如果需要,您还可以指定一个特定版本(例如1.8.2):

pip install django==1.8.2

Either way you'll have the correct version of Django in your virtual environment and it should work as you expect then. 无论哪种方式,您都将在虚拟环境中拥有正确版本的Django,并且它应该会按预期工作。

You can also use the following command to see what version you have installed: 您还可以使用以下命令查看已安装的版本:

pip show django

Update: 更新:

It seems that you have the correct version installed in your virtual environment, but for some reason your Windows 7 use the system Django installation instead while you use manage.py or django-admin.py directly . 似乎您在虚拟环境中安装了正确的版本,但是由于某些原因,Windows 7在直接使用manage.pydjango-admin.py时使用系统Django安装。 However, you can use python manage.py or python django-admin.py instead, which seems to work as expected (and use the virtualenv Django installation). 但是,您可以改用python manage.pypython django-admin.py ,它们似乎可以按预期工作(并使用virtualenv Django安装)。

Create a virtual environment for a project: 为项目创建虚拟环境:

$ mkdir cd my_project_folder
$ cd my_project_folder 
$ virtualenv venv
$ source venv/bin/activate

And now install django 现在安装django

(venv) ~$ pip install django==1.8

manage.py runserver in virtualenv using wrong django version 使用错误的Django版本的virtualenv中的manage.py runserver

pip / virtualenv / django installation issue pip / virtualenv / Django安装问题

I had the same problem. 我有同样的问题。 Could be related to your zsh/bash settings. 可能与您的zsh / bash设置有关。

I realized that using zsh (my default) I would get django-admin version 1.11 despite the Django version was 2.1! 我意识到使用zsh(我的默认设置)将获得django-admin版本1.11,尽管Django版本为2.1! When I tried the same thing with bash I would get django-admin version 2.1 (the correct version). 当我使用bash尝试相同的操作时,我将获得django-admin 2.1版(正确的版本)。 Certainly a misconfiguration. 当然是错误的配置。

So, I strongly suggest you check your zsh or bash settings to check for paths you might have. 因此,强烈建议您检查zsh或bash设置以检查可能具有的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM