简体   繁体   English

Python3作为默认python版本

[英]Python3 as default python version

I am creating a script that will call an API and return some results. 我正在创建一个脚本,该脚本将调用API并返回一些结果。 I have the script working with pycharm on my computer but I am running into a few problems but I want to focus on this problem first. 我的计算机上使用了pycharm脚本,但是遇到了一些问题,但是我想首先关注这个问题。

1) I am unable to set Python3 as my default python. 1)我无法将Python3设置为默认python。

I am using a Mac. 我正在使用Mac。 When I go into terminal I enter $ python --version and it returns Python 2.7.10 当我进入终端时,输入$ python --version ,它返回Python 2.7.10

I then enter $ alias python=python3 , and when I run $python --version it returns Python 3.7.2 然后输入$ alias python=python3 ,当我运行$python --version它将返回Python 3.7.2。

When I create a py.script with the os module, it does not work. 当我使用os模块创建py.script时,它不起作用。 See my code below. 请参阅下面的代码。

import os
os.system('alias python=python3')
print(os.system('python --version')

It prints 2.7.10 打印2.7.10

I also tried to run the os.system('alias python="python3"') 我也尝试运行os.system('alias python="python3"')

On -nix machines (including OSX), one way to change the version of the interpreter that the script runs with is to add a shebang as the first line of your script. 在-nix机器(包括OSX)上,更改脚本运行的解释器版本的一种方法是添加一个shebang作为脚本的第一行。

Eg. 例如。

#! /usr/bin/env python3

import sys
print(sys.version)

Then to run your script do: 然后运行脚本:

~/$ chmod u+x myscript.py
~/$ ./myscript.py 

You only need to run the chmod command the first time. 您只需要在第一次运行chmod命令。 It enables you to execute the file. 它使您能够执行文件。 Whenever you run your script directly (rather than as an argument to python) your script will be run using the version specified by the shebang. 每当您直接运行脚本(而不是作为python的参数)时,您的脚本都将使用shebang指定的版本运行。

welcome to SO! 欢迎来到! Pycharm needs you to specify which interpreter to use as default, as it wouldn't choose the system one by default. Pycharm需要您指定默认使用哪个解释器,因为默认情况下它不会选择系统之一。

So if you want python3 , you can run which python3 , and use the path as a settings for the current project. 因此,如果需要python3 ,则可以运行which python3 ,并将路径用作当前项目的设置。 How to do that step by step is here: 如何逐步执行此操作:

https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html

Hope it help, post a comment if you need more details. 希望对您有所帮助,如果您需要更多详细信息,请发表评论。

This isn't surprising, because os.system opens its own shell, and running alias in that way only affects the currently running terminal. 这并不奇怪,因为os.system打开了自己的shell,并且以这种方式运行alias只会影响当前正在运行的终端。 Each call to os.system would be in a separate shell. 每次对os.system调用都将在单独的外壳中。

I'm not sure what your ultimate goal is, but you almost certainly don't need to change what python means to a shell to do it. 我不确定您的最终目标是什么,但是几乎可以肯定,您不需要将python含义更改为shell即可。 If you DO, you'll have to run both commands at once. 如果这样做,则必须一次运行两个命令。

import subprocess

cp = subprocess.run("alias python=python3 && /path/to/script")

Interesting - apparently os.system ignores the alias? 有趣-显然os.system忽略了别名? Just checked it in Linux and got the same results. 只需在Linux中检查它,即可获得相同的结果。

Try sys instead of os : 尝试使用sys而不是os

import sys
print(sys.version)

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

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