简体   繁体   English

在MacOS Sierra上使用Python 2.7和3.6解决冲突

[英]Resolve Conflicts with Python 2.7 & 3.6 on MacOS Sierra

I am just starting to teach myself Python. 我刚刚开始自学Python。 I'm having trouble getting it set up. 我在设置它时遇到了麻烦。

I'm on a MacBook Pro/Sierra. 我在MacBook Pro / Sierra上。 I'm moderately comfortable in command line. 我在命令行中比较满意。

What I'd like to do: run Python 3.6 through Geany. 我想做的是:通过Geany运行Python 3.6。 Also, I want to install some libraries (eg, Requests) and have them work by default in 3.6. 另外,我想安装一些库(例如,请求),并使其在3.6中默认工作。

Here's what I've done, in chronological order: 这是我按时间顺序执行的操作:

  1. Downloaded/Installed Geany (using dmg package) 下载/安装的Geany(使用dmg软件包)
  2. Downloaded/Installed Python 3.6 (from dmg on python.org) 下载/安装了Python 3.6(来自python.org上的dmg)

The rest of what I did is in my shell session history below: 我所做的其余工作在下面的Shell会话历史记录中:

Last login: Thu Sep  7 19:52:33 on ttys002
$ alias python = 'python3.6'
-bash: alias: python: not found
-bash: alias: =: not found
-bash: alias: python3.6: not found
$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
$ python3
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ alias python = 'python3.6.2'
-bash: alias: python: not found
-bash: alias: =: not found
-bash: alias: python3.6.2: not found
$ nano .bash_profile
$ 
  [Restored Sep 7, 2017, 8:16:26 PM]
Last login: Thu Sep  7 20:16:20 on console
Restored session: Thu Sep  7 20:08:21 EDT 2017
$ python -V
Python 2.7.10
$ nano .bash_profile
$ nano ~/.bashrc
$ source ~/.bashrc
$ python
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ nano ~/.bash_profile
$ nano ~/.bashrc
$ source ~/.bashrc
$ python
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ brew install python3
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (caskroom/cask, homebrew/core).
==> Updated Formulae
node-build                               vagrant-completion

==> Installing dependencies for python3: sqlite
==> Installing python3 dependency: sqlite
==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.20.1.sierra.bottle
######################################################################## 100.0%
==> Pouring sqlite-3.20.1.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS provides an older sqlite3.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/sqlite/lib
    CPPFLAGS: -I/usr/local/opt/sqlite/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/sqlite/lib/pkgconfig

==> Summary
🍺  /usr/local/Cellar/sqlite/3.20.1: 11 files, 3.0MB
==> Installing python3
==> Downloading https://homebrew.bintray.com/bottles/python3-3.6.2.sierra.bottle
######################################################################## 100.0%
==> Pouring python3-3.6.2.sierra.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/2to3-3.6
Target /usr/local/bin/2to3-3.6
already exists. You may want to remove it:
  rm '/usr/local/bin/2to3-3.6'

To force the link and overwrite all conflicting files:
  brew link --overwrite python3

To list all files that would be deleted:
  brew link --overwrite --dry-run python3

Possible conflicting files are:
/usr/local/bin/2to3-3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/2to3-3.6
/usr/local/bin/idle3 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/idle3
/usr/local/bin/idle3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/idle3.6
/usr/local/bin/pydoc3 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/pydoc3
/usr/local/bin/pydoc3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/pydoc3.6
/usr/local/bin/python3 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
/usr/local/bin/python3-config -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3-config
/usr/local/bin/python3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
/usr/local/bin/python3.6-config -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6-config
/usr/local/bin/python3.6m -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m
/usr/local/bin/python3.6m-config -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config
/usr/local/bin/pyvenv -> /Library/Frameworks/Python.framework/Versions/3.6/bin/pyvenv
/usr/local/bin/pyvenv-3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/pyvenv-3.6
==> /usr/local/Cellar/python3/3.6.2/bin/python3 -s setup.py --no-user-cfg instal
==> /usr/local/Cellar/python3/3.6.2/bin/python3 -s setup.py --no-user-cfg instal
==> /usr/local/Cellar/python3/3.6.2/bin/python3 -s setup.py --no-user-cfg instal
==> Caveats
Pip, setuptools, and wheel have been installed. To update them
  pip3 install --upgrade pip setuptools wheel

You can install Python packages with
  pip3 install <package>

They will install into the site-package directory
  /usr/local/lib/python3.6/site-packages

See: https://docs.brew.sh/Homebrew-and-Python.html
==> Summary
🍺  /usr/local/Cellar/python3/3.6.2: 3,598 files, 55.9MB
$ python --version
Python 3.6.2
$ pip --version
-bash: pip: command not found
$ pip install -U pip
-bash: pip: command not found
$ python get-pip.py
Requirement already up-to-date: pip in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
Collecting wheel
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100% |████████████████████████████████| 71kB 2.2MB/s 
Installing collected packages: wheel
Successfully installed wheel-0.29.0
$ python -V
Python 3.6.2
$ pip install -U pip
-bash: pip: command not found
$ pip --version
-bash: pip: command not found
$ sudo easy_install pip
Password:
Searching for pip
Reading https://pypi.python.org/simple/pip/
Best match: pip 9.0.1
Downloading https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
Processing pip-9.0.1.tar.gz
Writing /tmp/easy_install-PJCCXY/pip-9.0.1/setup.cfg
Running pip-9.0.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-PJCCXY/pip-9.0.1/egg-dist-tmp-MiK6OH
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
  warnings.warn(msg)
warning: no previously-included files found matching '.coveragerc'
warning: no previously-included files found matching '.mailmap'
warning: no previously-included files found matching '.travis.yml'
warning: no previously-included files found matching '.landscape.yml'
warning: no previously-included files found matching 'pip/_vendor/Makefile'
warning: no previously-included files found matching 'tox.ini'
warning: no previously-included files found matching 'dev-requirements.txt'
warning: no previously-included files found matching 'appveyor.yml'
no previously-included directories found matching '.github'
no previously-included directories found matching '.travis'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'contrib'
no previously-included directories found matching 'tasks'
no previously-included directories found matching 'tests'
creating /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg
Extracting pip-9.0.1-py2.7.egg to /Library/Python/2.7/site-packages
Adding pip 9.0.1 to easy-install.pth file
Installing pip script to /usr/local/bin
Installing pip2.7 script to /usr/local/bin
Installing pip2 script to /usr/local/bin

Installed /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip
$ pip --version
pip 9.0.1 from /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)

Usage:   
  pip install [options] <requirement specifier> [package-index-options] ...
  pip install [options] -r <requirements file> [package-index-options] ...
  pip install [options] [-e] <vcs project url> ...
  pip install [options] [-e] <local project path> ...
  pip install [options] <archive url/path> ...

$ pip install --user pipenv
Collecting pipenv
  Downloading pipenv-6.1.4-py2-none-any.whl (665kB)
    100% |████████████████████████████████| 675kB 1.4MB/s 
Collecting semver (from pipenv)
  Downloading semver-2.7.8.tar.gz
Collecting setuptools>=36.3.0 (from pipenv)
  Downloading setuptools-36.4.0-py2.py3-none-any.whl (478kB)
    100% |████████████████████████████████| 481kB 1.5MB/s 
Collecting requests (from pipenv)
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 2.4MB/s 
Collecting pew>=0.1.26 (from pipenv)
  Downloading pew-0.1.26-py2.py3-none-any.whl
Collecting virtualenv (from pipenv)
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 546kB/s 
Collecting pip-tools>=1.9.0 (from pipenv)
  Downloading pip_tools-1.9.0-py2.py3-none-any.whl
Requirement already satisfied: pip in /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (from pipenv)
Collecting idna<2.7,>=2.5 (from requests->pipenv)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 6.3MB/s 
Collecting urllib3<1.23,>=1.21.1 (from requests->pipenv)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |████████████████████████████████| 133kB 3.2MB/s 
Collecting certifi>=2017.4.17 (from requests->pipenv)
  Downloading certifi-2017.7.27.1-py2.py3-none-any.whl (349kB)
    100% |████████████████████████████████| 358kB 1.4MB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests->pipenv)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 4.2MB/s 
Collecting pathlib; python_version == "2.7" (from pew>=0.1.26->pipenv)
  Downloading pathlib-1.0.1.tar.gz (49kB)
    100% |████████████████████████████████| 51kB 1.9MB/s 
Collecting backports.shutil-get-terminal-size; python_version == "2.7" (from pew>=0.1.26->pipenv)
  Downloading backports.shutil_get_terminal_size-1.0.0-py2.py3-none-any.whl
Collecting shutilwhich; python_version == "2.7" (from pew>=0.1.26->pipenv)
  Downloading shutilwhich-1.1.0.tar.gz
Collecting pythonz-bd>=1.10.2 (from pew>=0.1.26->pipenv)
  Downloading pythonz-bd-1.11.4.tar.gz (86kB)
    100% |████████████████████████████████| 92kB 4.6MB/s 
Collecting virtualenv-clone>=0.2.5 (from pew>=0.1.26->pipenv)
  Downloading virtualenv-clone-0.2.6.tar.gz
Requirement already satisfied: six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from pip-tools>=1.9.0->pipenv)
Collecting click>=6 (from pip-tools>=1.9.0->pipenv)
  Downloading click-6.7-py2.py3-none-any.whl (71kB)
    100% |████████████████████████████████| 71kB 2.8MB/s 
Collecting first (from pip-tools>=1.9.0->pipenv)
  Downloading first-2.0.1-py2.py3-none-any.whl
Installing collected packages: semver, setuptools, idna, urllib3, certifi, chardet, requests, pathlib, backports.shutil-get-terminal-size, virtualenv, shutilwhich, pythonz-bd, virtualenv-clone, pew, click, first, pip-tools, pipenv
  Running setup.py install for semver ... done
  Running setup.py install for pathlib ... done
  Running setup.py install for shutilwhich ... done
  Running setup.py install for pythonz-bd ... done
  Running setup.py install for virtualenv-clone ... done
Successfully installed backports.shutil-get-terminal-size-1.0.0 certifi-2017.7.27.1 chardet-3.0.4 click-6.7 first-2.0.1 idna-2.6 pathlib-1.0.1 pew-0.1.26 pip-tools-1.9.0 pipenv-6.1.4 pythonz-bd-1.11.4 requests-2.18.4 semver-2.7.8 setuptools-36.4.0 shutilwhich-1.1.0 urllib3-1.22 virtualenv-15.1.0 virtualenv-clone-0.2.6
$ cd Project
Project steve$ pipenv install requests
-bash: pipenv: command not found
Project steve$ pip install --user pipenv
Requirement already satisfied: pipenv in /Users/steve/Library/Python/2.7/lib/python/site-packages
Requirement already satisfied: semver in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pipenv)
Requirement already satisfied: setuptools>=36.3.0 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pipenv)
Requirement already satisfied: requests in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pipenv)
Requirement already satisfied: pew>=0.1.26 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pipenv)
Requirement already satisfied: virtualenv in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pipenv)
Requirement already satisfied: pip-tools>=1.9.0 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pipenv)
Requirement already satisfied: pip in /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (from pipenv)
Requirement already satisfied: idna<2.7,>=2.5 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from requests->pipenv)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from requests->pipenv)
Requirement already satisfied: certifi>=2017.4.17 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from requests->pipenv)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from requests->pipenv)
Requirement already satisfied: pathlib; python_version == "2.7" in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pew>=0.1.26->pipenv)
Requirement already satisfied: backports.shutil-get-terminal-size; python_version == "2.7" in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pew>=0.1.26->pipenv)
Requirement already satisfied: shutilwhich; python_version == "2.7" in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pew>=0.1.26->pipenv)
Requirement already satisfied: pythonz-bd>=1.10.2 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pew>=0.1.26->pipenv)
Requirement already satisfied: virtualenv-clone>=0.2.5 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pew>=0.1.26->pipenv)
Requirement already satisfied: six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from pip-tools>=1.9.0->pipenv)
Requirement already satisfied: click>=6 in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pip-tools>=1.9.0->pipenv)
Requirement already satisfied: first in /Users/steve/Library/Python/2.7/lib/python/site-packages (from pip-tools>=1.9.0->pipenv)
Project steve$ pipenv install requests
-bash: pipenv: command not found
Project steve$ python3.6 -m pipenv install
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6: No module named pipenv
Project steve$ python3.6 -m pip install
You must give at least one requirement to install (see "pip help install")
Project steve$ brew install intltool
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (caskroom/cask).
No changes to formulae.

==> Downloading https://homebrew.bintray.com/bottles/intltool-0.51.0.sierra.bott
######################################################################## 100.0%
==> Pouring intltool-0.51.0.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/intltool/0.51.0: 19 files, 185.9KB
Project steve$ python
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
Project steve$ pipenv --three
-bash: pipenv: command not found
Project steve$ python3.6 -m pipenv install
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6: No module named pipenv
Project steve$ 
$ brew link --overwrite --dry-run python3
Would remove:
/usr/local/bin/2to3-3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/2to3-3.6
/usr/local/bin/idle3 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/idle3
/usr/local/bin/idle3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/idle3.6
/usr/local/bin/pydoc3 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/pydoc3
/usr/local/bin/pydoc3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/pydoc3.6
/usr/local/bin/python3 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
/usr/local/bin/python3-config -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3-config
/usr/local/bin/python3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
/usr/local/bin/python3.6-config -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6-config
/usr/local/bin/python3.6m -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m
/usr/local/bin/python3.6m-config -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config
/usr/local/bin/pyvenv -> /Library/Frameworks/Python.framework/Versions/3.6/bin/pyvenv
/usr/local/bin/pyvenv-3.6 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/pyvenv-3.6
$ brew link --overwrite python3
Linking /usr/local/Cellar/python3/3.6.2... 19 symlinks created
$ rm '/usr/local/bin/2to3-3.6'
$ brew link --overwrite python3
Warning: Already linked: /usr/local/Cellar/python3/3.6.2
To relink: brew unlink python3 && brew link python3
$ brew unlink python3 && brew link python3
Unlinking /usr/local/Cellar/python3/3.6.2... 22 symlinks removed
Linking /usr/local/Cellar/python3/3.6.2... 23 symlinks created
$ pip3 install --upgrade pip setuptools wheel
Requirement already up-to-date: pip in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
Collecting setuptools
  Using cached setuptools-36.4.0-py2.py3-none-any.whl
Requirement already up-to-date: wheel in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
Installing collected packages: setuptools
  Found existing installation: setuptools 28.8.0
    Uninstalling setuptools-28.8.0:
      Successfully uninstalled setuptools-28.8.0
Successfully installed setuptools-36.4.0
$ python --version
Python 2.7.10
$ 

I'm a complete newbie in Python, and not a professional programmer. 我是Python的新手,而不是专业的程序员。 I researched this all afternoon yesterday. 我昨天整个下午都在研究。

Thanks for any help! 谢谢你的帮助!

Well, you've got some obvious problems with your shell commands here (that's why it's giving you an error): 好的,您的shell命令在这里有一些明显的问题(这就是为什么它会给您一个错误):

$ alias python = 'python3.6.2'
-bash: alias: python: not found
-bash: alias: =: not found
-bash: alias: python3.6.2: not found

You mean alias python=python3.6.2 (no spaces around = ). 你的意思是alias python=python3.6.2 (周围没有空格= )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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