Trying to understand ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable?

huangapple go评论54阅读模式
英文:

Trying to understand ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable?

问题

我特定的问题是,我如何修复这个导入错误,以便我可以运行python manage.py runserver命令?

由于某种原因,我的环境被删除了,我对此感到不满,但当我重新安装了一切后,出现了这些问题。

我在Stack Overflow上搜索了一些文章,但没有找到答案。

到目前为止,我已经:

我问ChatGPT什么是Python环境变量:

PYTHONPATH环境变量在Python中包含一个目录列表,Python在其中查找要导入的模块和包。它允许您指定Python在导入模块时应搜索的附加目录。

ChatGPT接着说运行pip install django。我已经这样做了,还激活了我的虚拟环境。

我如何查看我的Python环境变量?我如何将Django添加到其中?

有什么建议吗?

https://github.com/strikeouts27/mysite

英文:

My specific question is, how do I fix this import error so that I can run the python manage.py runsever command?

For some reason my environment did get deleted and I was not pleased about that but when I reinstalled everything I had these issues.

I have searched on stack overflow articles and come up empty.

So far I have:

I asked chat gpt what a python enviornment variable is:

The PYTHONPATH environment variable in Python contains a list of directories where Python looks for modules and packages to import. It allows you to specify additional directories that Python should search when importing modules.

It went on to say run pip install django. Which I have done, I also activated my virtual enviornment.

How do I see my python environment variables? How do I add Django to it?

Any advice?

https://github.com/strikeouts27/mysite

答案1

得分: 0

我认为创建新的虚拟环境比尝试从全局复制库更好(顺便说一下,不知道是否可能)。

  1. 创建虚拟环境 python -m venv /path/to/new/virtual/environment
  2. 在Windows上激活虚拟环境使用 venv\scripts\activate,在Linux上使用 venv/bin/activate.sh
  3. 现在你应该在控制台中看到 (venv) 字样变为绿色。
  4. 现在,你可以在你的虚拟环境中安装 Django,如果这不起作用,可以使用 python -m pip install django

希望这回答了你的问题。

英文:

I think it’s better to create new venv than try to copy lib from global (don’t know is it possible btw).

  1. Create venv python -m venv /path/to/new/virtual/environment
  2. Activate venv venv\scripts\activate for windows. venv/bin/activate.sh for Linux.
  3. Now you should see (venv) in green in your console.
  4. Well, now you can pip install django into your venv. If this doesn't work, use python -m pip install django.

Hope this answered your question.

答案2

得分: 0

显然,pip list 是一个我可以运行的命令,以查看Django是否在我的环境中。它似乎在它的列表中包含了Django。

我再次遇到了这个问题,我决定删除我的虚拟环境,并以我更熟悉的方式重新设置它。看起来它能够工作,但我希望它不会在以后引发问题。

运行了python -m pip install django,而且它有效。

似乎查看这个问题的方式是询问为什么pip或者其他安装它的工具没有安装它。

英文:

Apparently pip list is a command I can run to see if Django is in my environment. It seems Django is in the list of things it has.

I ran into it again, and I decided to remove my virtual environment and set it up another way that I am more familiar with. it seems to work but I hope it does not cause problems down the line.

python -m pip install django and that worked.

It seems that the way to view this is to ask why pip or whatever is installing it is not installing it.

huangapple
  • 本文由 发表于 2023年7月14日 04:56:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定