首页 > 解决方案 > pipenv shell中的ModuleNotFoundError?

问题描述

我最近正在处理的一个 Python 项目从使用 a virtualenvwith a切换requirements.txt到 using pipenv。根目录包含以下内容Pipfile

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
# AWS SDK for Python
boto3 = "==1.6.17"

# Use DATABASE_URL env variable to configure Django application
dj-database-url = "==0.4.2"

# Web framework
django = "==1.11.9"

# Django email integration for transactional ESPs
django-anymail = "==0.10"

# Log of changes made to a model
django-auditlog = "==0.4.5"

# Compresses linked and inline JavaScript or CSS into a single cached file
django-compressor = "==2.2"

# Save and retrieve current request object anywhere in your code
django-crequest = "==2018.5.11"

# Blocks people from brute forcing login attempts
django-defender = "==0.5.4"

# Wrap standard Django fields with encryption
django-encrypted-model-fields = "==0.5.3"

# Custom extensions for the Django Framework
django-extensions = "==2.0.0"

# A set of high-level abstractions for Django forms
django-formtools = "==2.1"

# Import and export data in multiple formats (Excel, CSV, JSON, and so on)
django-import-export = "==0.5.1"

# OAuth2 for Django
django-oauth-toolkit = "==1.0.0"

# SASS integration
django-sass-processor = "==0.5.7"

# Collection of custom storage backends for Django
django-storages = "==1.6.6"

# Two-Factor Authentication for Django
django-two-factor-auth = "==1.7.0"

# Tweak the form field rendering in templates
django-widget-tweaks = "==1.4.1"

# Toolkit for building Web APIs
djangorestframework = "==3.6.3"

# Fixtures replacement
factory-boy = "==2.10.0"

# Style Guide Enforcement
flake8 = "==3.5.0"

# Allows tests to travel through time by mocking the datetime module
freezegun = "==0.3.9"

# Python WSGI HTTP Server
gunicorn = "==19.7.1"

# Newrelic adapter
newrelic = "==2.90.0.75"

# Parsing, formatting, and validating international phone numbers
phonenumbers = "==8.9.1"

# Imaging processing library
pillow = "==5.0.0"

# PostgreSQL adapter
psycopg2 = "==2.7.1"

# Python exception notifier for Airbrake
pybrake = "==0.3.3"

# ISO databases for languages, countries and subdivisions
pycountry = "==18.2.23"

# Extensions to the standard datetime module
python-dateutil = "==2.6.0"

# Loads environment variables from .env file
python-dotenv = "==0.7.1"

# HTTP library
requests = "==2.19.1"

# Python library to capitalize strings
titlecase = "==0.12.0"

# Communication with the Twilio API
twilio = "==6.4.3"

# Static file serving
whitenoise = "==3.3.1"

[dev-packages]

[requires]
python_version = "3.7.0"

如您所见,包包括python-dotenv,用于我们的 Django 项目的manage.py. 但是,如果我激活pipenv shell

Kurts-MacBook-Pro-2:lucy-web kurtpeek$ pipenv shell
Loading .env environment variables...
Creating a virtualenv for this project...
Pipfile: /Users/kurtpeek/Documents/Dev/lucy2/lucy-web/Pipfile
Using /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7m (3.7.0) to create virtualenv...
⠋Running virtualenv with interpreter /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7m
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
/usr/local/Cellar/pipenv/2018.7.1/libexec/lib/python3.7/site-packages/virtualenv.py:1041: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
New python executable in /Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/bin/python3.7m
Also creating executable in /Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/bin/python
Installing setuptools, pip, wheel...done.
Setting project for lucy-web-CVxkrCFK to /Users/kurtpeek/Documents/Dev/lucy2/lucy-web

Virtualenv location: /Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK
Launching subshell in virtual environment…
bash-3.2$  . /Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/bin/activate
(lucy-web-CVxkrCFK) bash-3.2$

然后尝试运行python manage.py shell,我得到一个ModuleNotFoundErrorfor dotenv

(lucy-web-CVxkrCFK) bash-3.2$ python manage.py shell
Traceback (most recent call last):
  File "manage.py", line 4, in <module>
    from dotenv import load_dotenv, find_dotenv
ModuleNotFoundError: No module named 'dotenv'

另外,如果我执行pip freeze命令,我看不到任何安装的包:

(lucy-web-CVxkrCFK) bash-3.2$ pip freeze
(lucy-web-CVxkrCFK) bash-3.2$ 

pipenv shell不应该已经安装了吗packagesPipfile还是我需要做一个额外的步骤?

标签: pythonpipenv

解决方案


事实证明,您确实需要运行pipenv install才能实际安装软件包。该pipenv shell命令仅激活虚拟环境。


推荐阅读