我有两个版本的python安装了2.7和3.4并创建了一个virtualenv并为这个新环境分配了python34。当我激活virtualenv并运行python manage.py runserver
时,我得到以下输出。
(casenv) C:\pyprojects\focus\site>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\virtualenvs\casenv\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\virtualenvs\casenv\lib\site-packages\django\core\management\__init__.py", line 341, in execute
django.setup()
File "C:\virtualenvs\casenv\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\virtualenvs\casenv\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "C:\virtualenvs\casenv\lib\site-packages\django\apps\config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "C:\virtualenvs\casenv\lib\importlib\__init__.py", line 104, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1448, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\pyprojects\focus\site\general\models.py", line 8, in <module>
from focus2.util import HashedPk
File "C:\pyprojects\focus\site\focus2\util.py", line 3, in <module>
from settings import Hasher
ImportError: No module named 'settings'
文件 - manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "focus2.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
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?"
)
raise
execute_from_command_line(sys.argv)
utils.py的前两行 -
from django.utils.html import strip_tags, escape
from settings import Hasher
项目目录结构是
├───focus
│ ├───data_dumps
│ ├───notes
│ ├───setup
│ └───site(main project folder)
| └───static
| └───general
| └───pro
| └───models
| +--__init__.py
| +--plans.py
│ └───focus2
│ └───templates
| +--__init__.py
│ +--settings.py
│ +--util.py
│ +--wsgi.py
│ +--manage.py
我有相同的代码和目录结构,与virtualenv python2.7运行良好。 python27 env和python34 env之间的区别在于我在27中安装了mysql-python模块,在34中安装了mysqlclient。
来自python34 env。
的Pip冻结命令(casenv) C:\virtualenvs\casenv>pip freeze
certifi==2017.7.27.1
chardet==3.0.4
defusedxml==0.5.0
Django==1.10.1
django-ajax-selects==1.5.0
django-allauth==0.32.0
django-crispy-forms==1.6.1
easy-thumbnails==2.3
hashids==1.1.0
idna==2.5
lxml==3.7.3
mysqlclient==1.3.10
oauthlib==2.0.2
Pillow==3.3.1
PyMySQL==0.7.11
python3-openid==3.1.0
requests==2.18.3
requests-oauthlib==0.8.0
urllib3==1.22
来自python27 virtualenv的Pip冻结命令。运行这个venv时我没有收到任何错误。
(testvenv27) C:\virtualenvs\testvenv27>pip freeze
Django==1.10.1
django-ajax-selects==1.5.0
django-allauth==0.27.0
django-crispy-forms==1.6.0
easy-thumbnails==2.3
hashids==1.1.0
lxml==3.4.2
MySQL-python==1.2.5
oauthlib==2.0.0
Pillow==3.3.1
python-openid==2.2.5
requests==2.9.1
requests-oauthlib==0.6.1
sys.path命令输出 -
>>> import sys
>>> print(sys.path)
['', 'C:\\WINDOWS\\SYSTEM32\\python34.zip', 'C:\\virtualenvs\\casenv\\DLLs', 'C:\\virtualenvs\\casenv\\lib', 'C:\\virtualenvs\\casenv\\Scripts', 'c:\\python34\\Lib', 'c:\\python34\\DLLs', 'C:\\virtualenvs\\casenv', 'C:\\virtualenvs\\casenv\\lib\\site-packages']
非常感谢任何帮助。
答案 0 :(得分:3)
尝试:
from .settings import Hasher