简体   繁体   English

Django 找不到新模块,使用 pyenv 和 virtualenv

[英]Django cannot find new modules, using pyenv and virtualenv

I'm sure this is pretty straightforward to someone experienced.我敢肯定,这对于有经验的人来说非常简单。 I'm learning Django through the Wedge of Django ebook.我正在通过 Django 电子书的 Wedge 学习 Django。

I'm using Python 3.8.7 installed via pyenv like so: pyenv install 3.8.7我正在使用通过 pyenv 安装的 Python 3.8.7,如下所示: pyenv install 3.8.7

Then I've set up a virtualenv like so: pyenv virtualenv 3.8.7 everycheese然后我像这样设置了一个virtualenv: pyenv virtualenv 3.8.7 everycheese

I activate the virtualenv in my repo like so: pyenv local everycheese我像这样在我的仓库中激活 virtualenv: pyenv local everycheese

The environment is shown as active in the prompt, as it starts with (everycheese).环境在提示中显示为活动的,因为它以 (everycheese) 开头。

The main project is cloned from Django Cookiecutter https://github.com/cookiecutter/cookiecutter-django主项目克隆自Django Cookiecutter https://github.com/cookiecutter/cookiecutter-django

I've then used pip to install requirements from the requirements.txt files.然后我使用 pip 从 requirements.txt 文件中安装要求。

However - I'm running into trouble when I try to add new packages (by adding the package to requirements.txt as a new line and installing again with pip).但是 - 当我尝试添加新软件包时遇到麻烦(通过将 package 作为新行添加到 requirements.txt 并使用 pip 再次安装)。

pip list , or pip freeze both show the new module. pip listpip freeze都显示新模块。 But when I add the module to my INSTALLED_APPS and try to import it in my models.py file, Django cannot find it.但是当我将模块添加到我的 INSTALLED_APPS 并尝试将其导入到我的 models.py 文件中时,Django 找不到它。

When I type which python , and which pip , they point to different directories and I think this may be part of the problem but I am stuck.当我输入which pythonwhich pip时,它们指向不同的目录,我认为这可能是问题的一部分,但我被卡住了。

When using pip (or actually any other Python script), it is important to make sure which Python interpreter is used.使用pip (或实际上任何其他 Python 脚本)时,确保使用哪个 Python 解释器非常重要。 Usually it is obvious which Python interpreter is used when calling pip .通常在调用pip时使用哪个 Python 解释器是显而易见的。 But sometimes it is not clear, and the script is actually running with a different interpreter that one might think.但有时并不清楚,脚本实际上是使用人们可能认为的不同解释器运行的。 Which leads to unexpected results and a lot of confusion.这会导致意想不到的结果和很多混乱。

Therefore it is always better to call explicitly the exact Python interpreter you are targeting and tell it to run pip 's executable module (or any other executable module).因此,最好明确调用您所针对的确切 Python 解释器并告诉它运行pip可执行模块(或任何其他可执行模块)。 Typically:通常:

$ python -m pip install Something
$ # instead of 'pip install Something'
$ python3 -m pip install Something
$ # instead of 'pip3 install Something'

If there is still doubt, one could even go one step further and use a more explicit path to the Python interpreter explicitly:如果仍有疑问,甚至可以进一步使用 go 并使用更明确的 Python 解释器路径:

  • /the/path/to/my/pythonX.Y -m pip
  • path/to/my/pythonX.Y -m pip
  • path/to/my/python -m pip
  • path/to/venv/bin/python -m pip
  • pythonX.Y -m pip
  • python3 -m pip
  • python -m pip

Resource :资源

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

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