我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'
这是预期的行为,您实际上并不需要 PyCharm 将您的虚拟环境视为项目代码的一部分。
确保您创建的 venv 在Settings |中被选为项目解释器。项目... | 项目解释器,它安装了 Django。
这些是结果
print(sys.path)
您的结果
print(sys.path)
显示了许多用户进行的修改,可能是您的 Ubuntu 19.04 中多个不推荐的包管理决策的结果。