How to use Python 3 (3.5) as the default interpreter in my virtual environment?

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

I ran into problems when I installed Python 3.5 on Mac. I wanted to use Python 3.5 as the interpreter when I ran my Django development server. I got this issue when I tried it (I know I must install a virtualenv, read below):

$ python3 manage.py runserver Traceback (most recent call last):   File "manage.py", line 7, in <module>     from mezzanine.utils.conf import real_project_name   File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Mezzanine-4.0.1-py3.5.egg/mezzanine/utils/conf.py", line 7, in <module>     from django.conf import global_settings as defaults ImportError: No module named 'django' 

The first was when I tried installing pip packages it installed by default on the Python 2.7 in my virtual environment:

I use Django so I tried also install a virtual environment for the first time. First I tried it with pip, but that didn't work.

So how do I setup so that my Python3.5 is the default usage when using Python interpreter and in my virtual environment? I can't figure this out or I have done something wrong in my process.

Edit:

Thanks for the answer and comment. I tried with the both solutions and the same error persisted. See output below:

$ virtualenv -p /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 Django/ Running virtualenv with interpreter /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 Traceback (most recent call last):   File "/Library/Python/2.7/site-packages/virtualenv.py", line 14, in <module>     import shutil   File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shutil.py", line 10, in <module>     import fnmatch   File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/fnmatch.py", line 15, in <module>     import functools   File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/functools.py", line 21, in <module>     from collections import namedtuple   File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/collections/__init__.py", line 16, in <module>     from reprlib import recursive_repr as _recursive_repr   File "/Library/Python/2.7/site-packages/reprlib/__init__.py", line 7, in <module>     raise ImportError('This package should not be accessible on Python 3. ' ImportError: This package should not be accessible on Python 3. Either you are trying to run from the python-future src folder or your installation of python-future is corrupted. 

My virtualenv version: 13.1.2

回答1:

Your error is related to a known virtualenv bug regarding python-future and mixing up the 2.7 and 3.x module paths, the solution is to downgrade virtualenv to a version < 12.04, 12.0.2 is suggested in the link.



回答2:

Firstly, If you are going to use multiple versions of python,then you must use Virtual Env.Also then Create your virtualenv using the following command so that it uses python 3.5 as default environment :

          virtualenv -p python3 envname 

and then install django in the virtualenv.after activating your virtualenv



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!