Ich habe Python 3.2 in meinem Mac installiert. Nachdem ich/Applications/Python 3.2/Update Shell Profile.command ausgeführt habe, ist es verwirrend, dass ich, wenn ich python -V in Terminal eingebe, besagt, dass Python 2.6.1 , wie kann ich das ändern die Standard-Python-Version?
Dies ist wahrscheinlich für die Rückwärtskompatibilität wünschenswert.
Python3 bricht die Rückwärtskompatibilität ab, und Programme, die 'Python' aufrufen, erwarten wahrscheinlich Python2. Sie haben wahrscheinlich viele Programme und Skripte, von denen Sie nicht einmal wissen, welche davon ausgehen, dass Python = Python2 erwartet wird, und wenn Sie dies ändern, werden diese Programme und Skripts beschädigt.
Die Antwort, die Sie wahrscheinlich suchen, ist Sie sollten dies nicht ändern .
Sie können jedoch einen benutzerdefinierten Alias in Ihrer Shell festlegen. Die Art und Weise, wie Sie dies tun, hängt von der Shell ab. Möglicherweise können Sie jedoch alias py=python3
Wenn Sie beim Starten der neuesten Version von Python verwirrt sind, ist es unter Linux zumindest der Fall, dass python3
Ihre python2
-Installation intakt lässt (aus den oben genannten Kompatibilitätsgründen); Daher können Sie python3 mit dem Befehl python3
starten.
Unter Mac OS X, das das Installationsprogramm von python.org verwendet, wie Sie es anscheinend haben, müssen Sie Python 3 mit python3
und nicht python
aufrufen. Das ist derzeit für Python 2-Versionen reserviert. Sie können auch python3.2
verwenden, um diese Version gezielt aufzurufen.
$ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ cd /Library/Frameworks/Python.framework/Versions/3.2/bin/
$ ls -l
total 384
lrwxr-xr-x 1 root admin 8 Apr 28 15:51 [email protected] -> 2to3-3.2
-rwxrwxr-x 1 root admin 140 Feb 20 11:14 2to3-3.2*
lrwxr-xr-x 1 root admin 7 Apr 28 15:51 [email protected] -> idle3.2
-rwxrwxr-x 1 root admin 138 Feb 20 11:14 idle3.2*
lrwxr-xr-x 1 root admin 8 Apr 28 15:51 [email protected] -> pydoc3.2
-rwxrwxr-x 1 root admin 123 Feb 20 11:14 pydoc3.2*
-rwxrwxr-x 2 root admin 25624 Feb 20 11:14 python3*
lrwxr-xr-x 1 root admin 12 Apr 28 15:51 [email protected] -> python3.2-32
lrwxr-xr-x 1 root admin 16 Apr 28 15:51 [email protected] -> python3.2-config
-rwxrwxr-x 2 root admin 25624 Feb 20 11:14 python3.2*
-rwxrwxr-x 1 root admin 13964 Feb 20 11:14 python3.2-32*
lrwxr-xr-x 1 root admin 17 Apr 28 15:51 [email protected] -> python3.2m-config
-rwxrwxr-x 1 root admin 25784 Feb 20 11:14 python3.2m*
-rwxrwxr-x 1 root admin 1865 Feb 20 11:14 python3.2m-config*
lrwxr-xr-x 1 root admin 10 Apr 28 15:51 [email protected] -> pythonw3.2
lrwxr-xr-x 1 root admin 13 Apr 28 15:51 [email protected] -> pythonw3.2-32
-rwxrwxr-x 1 root admin 25624 Feb 20 11:14 pythonw3.2*
-rwxrwxr-x 1 root admin 13964 Feb 20 11:14 pythonw3.2-32*
Wenn Sie auch ein Python 2 von python.org installiert haben, hätte es ein ähnliches Framework-Verzeichnis mit keinen überlappenden Dateinamen (außer 2to3).
$ open /Applications/Python\ 2.7/Update\ Shell\ Profile.command
$ sh -l
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ cd /Library/Frameworks/Python.framework/Versions/2.7/bin
$ ls -l
total 288
-rwxrwxr-x 1 root admin 150 Jul 3 2010 2to3*
lrwxr-x--- 1 root admin 7 Nov 8 23:14 [email protected] -> idle2.7
-rwxrwxr-x 1 root admin 138 Jul 3 2010 idle2.7*
lrwxr-x--- 1 root admin 8 Nov 8 23:14 [email protected] -> pydoc2.7
-rwxrwxr-x 1 root admin 123 Jul 3 2010 pydoc2.7*
lrwxr-x--- 1 root admin 9 Nov 8 23:14 [email protected] -> python2.7
lrwxr-x--- 1 root admin 16 Nov 8 23:14 [email protected] -> python2.7-config
-rwxrwxr-x 1 root admin 33764 Jul 3 2010 python2.7*
-rwxrwxr-x 1 root admin 1663 Jul 3 2010 python2.7-config*
lrwxr-x--- 1 root admin 10 Nov 8 23:14 [email protected] -> pythonw2.7
-rwxrwxr-x 1 root admin 33764 Jul 3 2010 pythonw2.7*
lrwxr-x--- 1 root admin 11 Nov 8 23:14 [email protected] -> smtpd2.7.py
-rwxrwxr-x 1 root admin 18272 Jul 3 2010 smtpd2.7.py*
Alte Frage, aber alternativ:
virtualenv --python=python3.5 .venv
source .venv/bin/activate
Ändern Sie den "Standard" -Python, indem Sie ihn vor dem System-Python in Ihrem Pfad platzieren.
export PATH=/usr/local/bin:$PATH
Überprüfen Sie die Position von Python 3
$ which python3
/usr/local/bin/python3
Schreiben Sie einen Alias in bash_profile
vi ~/.bash_profile
alias python='/usr/local/bin/python3'
Laden Sie bash_profile neu
source ~/.bash_profile
Bestätigen Sie den Python-Befehl
$ python --version
Python 3.6.5
Ich verwende OS X 10.7.5 und Python 3.4.2. Wenn Sie python3
eingeben und ausführen möchten, wird es mit Python 3 ausgeführt. Zum Beispiel pyhton3 test001.py
. Daraufhin lief ein Testprogramm namens test001. Ich hoffe das hilft.
Gemäß einer schnellen Google-Suche gilt dieses Update nur für die aktuell geöffnete Shell. Es kann wahrscheinlich durch die Eingabe von python3 behoben werden, da mac und linux so ähnlich sind, dass Dinge wie dieses zusammenfallen. Link zum Ergebnis der Google-Suche.
Wie Ninjagecko bereits feststellte, wurden die meisten Programme noch nicht auf 3.x aktualisiert. Wenn Sie also den Standard-Python als 3.x verwenden, würden viele in Anwendungen verwendete Python-Skripts beschädigt.
Navigiere zu:
Arbeitsplatz -> Eigenschaften -> Erweitert -> Umgebungsvariablen -> Systemvariablen
Angenommen, Sie hatten bereits Python 2.7 in der Pfadvariablen hinzugefügt, und Sie möchten den Standardpfad in Python 3.x ändern
fügen Sie dann den Pfad des Python3.5.x-Ordners vor dem Python2.7-Pfad hinzu.
open Cmd: Typ "Python - Version"
die Python-Version wird in Python 3.5.x geändert
Sudo mv /usr/bin/python /usr/bin/python2
Sudo ln -s $(which python3) /usr/bin/python
Dies wird Skripte brechen, ist aber genau der Weg, um Python zu ändern. Sie sollten die Skripte auch so umschreiben, dass nicht angenommen wird, dass python
2.x ist. Dies funktioniert unabhängig davon, wo Sie system
oder exec
aufrufen.
Mach das Richtige, mach das Richtige!
---> null Öffnen Sie Ihr Terminal,
--Erste Eingabe python -V
, zeigt wahrscheinlich:
Python 2.7.10
-Eine zweite Eingabe python3 -V
, zeigt wahrscheinlich:
Python 3.7.2
- Drittens geben Sie where python
oder which python
ein. Es zeigt wahrscheinlich:
/usr/bin/python
--- Viertens geben Sie where python3
oder which python3
ein. Es zeigt wahrscheinlich:
/usr/local/bin/python3
- Fügen Sie fünftens die folgende Zeile am Ende Ihrer PATH-Umgebungsvariablendatei in ~/.profile file or ~/.bash_profile
unter Bash oder ~/.zshrc
unter zsh hinzu.
alias python='/usr/local/bin/python3'
OR
alias python=python3
- Geben Sie source ~/.bash_profile
unter Bash oder source ~/.zshrc
unter zsh ein.
- Endlich das Terminal verlassen.
--- Achtes Öffnen Sie Ihr Terminal und geben Sie python -V
ein. Es zeigt wahrscheinlich:
Python 3.7.2
Ich hatte es erfolgreich ausprobiert.
Andere, der ~/.bash_profile
unter zsh ist nicht der ~/.bash_profile
.
Die PATH-Umgebungsvariable unter zsh statt ~/.profile
(oder ~/.bash_file
) über ~/.zshrc
.
Helft euch!