我的PyCharm IDE
不会自动创建venv directory
,所以我必须手动创建自己的。我的venv directory
被忽略了。这是正常的。在评论部分可以找到一个可能的解决方案。创建PyCharm Project
Django
和virtualenv
安装时。我做了不是必须安装Django
。所以现在Python3
需要清理。
更新:我遇到这个问题已经有PyCharm IDE
一段时间了,我想确定的是sys.path
:来自PyCharm IDE terminal
:
>>> python3
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/name/Projects/bye/venv/lib/python3.7/site-packages', '/home/name/Projects
/bye/venv/lib/python3.7/site-packages/setuptools-40.8.0-py3.7.egg', '/home/name/Projects/bye/venv/lib/python3.7/site-packages/pip-19.0.3-py3.7.egg']
尽管目录已创建,但我仍然得到此结果:
ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
ImportError 是由以下 Python 3 代码的第 6 行生成的,不一定是由代码生成的,而是由from django.core.management import
第 4 行生成的。
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Example.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Example.settings')
application = get_wsgi_application()
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Example.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'Example.wsgi.application'
答案1
这些是print(sys.path)
$python3 >>> 导入系统 >>> 打印(系统路径) ['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
结果print(sys.path)
显示大量用户所做的修改,可能是由于 Ubuntu 19.04 中多个不推荐的包管理决策造成的。
答案2
我的 venv 目录被忽略
这是预期的行为,您实际上不需要 PyCharm 将您的虚拟环境视为项目代码的一部分。
尽管目录已创建,但我仍然得到此结果:
确保您创建的 venv 被选为项目解释器设置|项目...|项目解释器并且已经安装了 Django。