简体   繁体   中英

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

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:

pip install django==1.9rc2
Collecting django==1.9rc2
  Downloading Django-1.9rc2-py2.py3-none-any.whl (6.4MB)
    100% |████████████████████████████████| 6.4MB 66kB/s 
Installing collected packages: django
Successfully installed django-1.9rc2

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

您的错误与已知的有关python-future virtualenv 错误有关,并混合了2.7和3.x模块路径,解决方案是将virtualenv降级到<12.04,建议在链接中使用12.0.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM