Python Pycharm 报错 Django is not importable in this environment

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27363661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:42:53  来源:igfitidea点击:

Pycharm error Django is not importable in this environment

pythondjangopycharm

提问by Pablo Lorenzo

I'm getting the following errors in Pycharm.

我在 Pycharm 中收到以下错误。

Error: Django is not importable in this environment

错误:Django 在此环境中不可导入

It used to work well but I moved the project and virtualenv to other directory and the error appeared. It's weird because if I run the project ignoring the configuration settings warning that I get, works fine.

它曾经运行良好,但我将项目和 virtualenv 移动到其他目录并出现错误。这很奇怪,因为如果我运行项目而忽略我收到的配置设置警告,则工作正常。

The problem is that now, Pycharm is not finding the project interperter.

问题是现在,Pycharm 没有找到项目互操作者。

The command that Pycharm is running is:

Pycharm 正在运行的命令是:

/Users/plorenzo/../gestorSchools/venv/bin/python3.4 /Users/plorenzo/../gestorSchools/schoolsManager/manage.py runserver 8000

The virtualenv is activated.

virtualenv 被激活。

My $PATH is:

我的 $PATH 是:

/Users/plorenzo/../gestorSchools/venv/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

and my $PYTHONPATH:

和我的 $PYTHONPATH:

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

I spent a few days trying to find the solution but I couldn't find it.

我花了几天时间试图找到解决方案,但我找不到。

采纳答案by Pablo Lorenzo

I finally found the error. The problem was that one folder name in the virtualenv path had an accent.

我终于找到了错误。问题是 virtualenv 路径中的一个文件夹名称带有重音。

This is the old path:

这是旧路径:

/Users/plorenzo/../telefónica/gestorSchools/venv

This is the new one that works:

这是一个有效的新方法:

/Users/plorenzo/../telefonica/gestorSchools/venv

回答by cbueche

I got the same issue on OS X, and to fix it, I had to use an absolute path for the project interpreter

我在 OS X 上遇到了同样的问题,要修复它,我必须为项目解释器使用绝对路径

/Users/myself/Projects/... is ok ~/Projects/... was not working

/Users/myself/Projects/... 没问题 ~/Projects/... 不工作

回答by tanaydin

I had similar problem...

我有类似的问题...

I go PyCharm -> Preferences and tried to readd Django to project interpreter packages. PyCharm suddenly added everything in requirements.txt file. And now I can run project without any problem.

我去 PyCharm -> Preferences 并尝试读取 Django 来项目解释器包。PyCharm 突然在 requirements.txt 文件中添加了所有内容。现在我可以毫无问题地运行项目。

回答by James Bradbury

In my case I had to go to Settings (Alt+F7, or the Spanner/Cog icon) and select a Python interpreter which has Django installed alongside it.

就我而言,我必须转到“设置”(Alt+F7,或 Spanner/Cog 图标)并选择一个安装了 Django 的 Python 解释器。

If Django isn't already installed, you should be able to install it via the green + icon (not shown below) once an interpreter has been selected.

如果尚未安装 Django,则在选择解释器后,您应该能够通过绿色 + 图标(下方未显示)安装它。

enter image description here

在此处输入图片说明

回答by Siyaram Malav

Project interpreter, which is being used in the pycharmsetting , doesn't have the djangopackage installed. So go to

pycharm设置中使用的项目解释器没有django安装包。所以去

setting->Project->project interpreter

Then install djangopackage

然后安装django

it will work :)

它会起作用:)

回答by Dakusan

In my case, I was running the Cygwin version of Python when this error came up. Fixing it involved just adding some of cygwin's bin paths to Window's Path variable.

就我而言,出现此错误时,我正在运行 Cygwin 版本的 Python。修复它只需要将一些 cygwin 的 bin 路径添加到 Window 的 Path 变量中。

Run "control sysdm.cpl" Go to the "Advanced" tab Click the "Environmental Variables" button at the bottom of this tab Find "Path" under "System Variables" Add to it ;c:/cygwin/bin;c:/cygwin/sbin;c:/cygwin/usr/local/bin

运行“control sysdm.cpl”转到“高级”选项卡单击此选项卡底部的“环境变量”按钮在“系统变量”下找到“路径”添加到其中 ;c:/cygwin/bin;c:/cygwin/sbin;c:/cygwin/usr/local/bin

I did not include /usr/bin, as for some reason, it is combined in to the /bin directory when viewing in windows.

我没有包含/usr/bin,因为某些原因,它在windows中查看时被合并到/bin目录中。

回答by Rob Osborne

Ok, in my case the solution was:

好的,就我而言,解决方案是:

  1. Install django
  2. Use @cbueche suggestion to remove the ~
  3. Switch to a another project's venv and switch back to my venv for this project
  1. 安装 Django
  2. 使用@cbueche 建议删除 ~
  3. 切换到另一个项目的 venv 并为此项目切换回我的 venv

I exited and reloaded the project a few times in there so that might also be necessary. I love PyCharm but starting a new project is always painful.

我退出并在那里重新加载了几次项目,因此这也可能是必要的。我喜欢 PyCharm,但开始一个新项目总是很痛苦。

回答by Trinadh Koya

As everybody said ,Check for your interpreterWas it properly configured it or not.If you created your project in virtual environment,make sure you configured the correct python interpreter or not .The python interpreter(whole) and python interpreter(for only single project using virtualenv) are different .

正如大家所说,检查你的解释器是否正确配置。如果你在虚拟环境中创建你的项目,请确保你配置了正确的python解释器。python解释器(整个)和python解释器(仅用于单个项目)使用 virtualenv) 是不同的。

If you are using PyCharm Go to Setting->Project->projectinterpreter and pick the right interpreter Once you are done with that ,run the project if it runs,all set .if not check for the Edit configurations and check again for the interpreterenter image description here

如果您正在使用 PyCharm 转到设置-> 项目-> 项目解释器并选择正确的解释器完成后,运行项目,如果它运行,所有设置。如果没有检查编辑配置并再次检查解释器在此处输入图片说明

回答by g.carey

It might seem obvious, but try quitting and then re-starting the PyCharm application. When I had this issue, I continued to get the "Django is not importable in this environment" error even though the project interpreter path was correct. Nothing worked until I closed and re-opened the application. Hope this saves someone some time.

这似乎很明显,但请尝试退出然后重新启动 PyCharm 应用程序。当我遇到这个问题时,即使项目解释器路径是正确的,我仍然收到“Django 在此环境中不可导入”错误。在我关闭并重新打开应用程序之前,没有任何效果。希望这可以节省一些时间。

回答by Arnaud P

Yet another case, where my virtualenv did have Django installed, but Pycharm would still display this error.

还有一种情况,我的 virtualenv 确实安装了 Django,但 Pycharm 仍会显示此错误。

I eventually found out that the same virtualenv path had been duplicated in the list of available interpreters. Removing all of them and re-adding fixed it.

我最终发现相同的 virtualenv 路径已在可用解释器列表中重复。删除所有这些并重新添加修复它。

The duplication happened after I copied the .idea from another machine, and re-created the virtualenv from Pycharm although it was already listed as a (broken) interpreter.

在我从另一台机器复制 .idea 并从 Pycharm 重新创建 virtualenv 之后发生了重复,尽管它已经被列为(损坏的)解释器。