linux中的python(linux安装python3)
很多朋友对于linux中的python和linux安装python3不太懂,今天就由小编来为大家分享,希望可以帮助到大家,下面一起来看看吧!
Linux系统上安装python详细步骤
在Linux系统上安装Python的详细步骤如下:
默认情况下,Linux环境中已经预装了Python,你可以通过运行`python--version`来确认当前版本。然而,如果你需要安装Python 3.10.7或其他特定版本,以下是一个具体的安装过程:
1.访问Python的官方源码下载页面:Python Source Releases| Python.org,选择你需要的版本(例如Python 3.10.7)。
2.下载并解压下载的tar.gz文件到Linux的某个目录中。例如,你可以使用`wget`下载并解压`python3.10.7.tgz`:
下载 python3.10.7.tgz
解压到目录:tar-xvf python3.10.7.tgz
3.进入解压后的目录,然后进行编译安装:
切换到目录:cd python3.10.7
编译安装:./configure--prefix=/muyun/python3.10
然后执行:make
4.安装完成后,为了区分Python 3.10和系统自带的Python 2,需要建立软连接:
创建软连接:sudo ln-s/muyun/python3.10/bin/python3/usr/bin/python3
同样为pip创建软连接:sudo ln-s/muyun/python3.10/bin/pip3/usr/bin/pip3
检查链接:ls-l/usr/bin/
5.若要让Python成为系统环境的一部分,需要更新环境变量。使用root权限打开`/etc/profile`或`.bashrc`,添加如下行:
添加到环境变量:export PATH=/muyun/python3.10/bin:$PATH
保存并执行:source~/.bashrc(如果在`.bashrc`中)或`source/etc/profile`(如果在`.profile`中)
如果在安装过程中遇到问题,例如:
-
错误1:缺少gcc编译环境。解决方法:yum-y install gcc,然后重新运行编译命令。
-
错误2:缺少libffi-devel。解决方法:yum install libffi-devel。
-
错误3:make过程中遇到模块缺失。对于不必要模块,忽略;对于关键模块如ssl,尝试:sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel make clean make。
遵循以上步骤,你就能成功在Linux上安装Python 3.10.7。记得根据实际情况调整路径和版本号。
在Linux Python环境中获取或更改当前工作目录的方法
本文介绍在Python环境中获取或更改当前工作目录的方法,以Linux平台为例。
在Python上处理目录中的文件时,建议使用绝对路径。但是,如果您使用的是相对路径,则需要了解当前工作目录的概念以及如何查找或更改当前工作目录。绝对路径指定从根目录开始的文件或目录位置,而相对路径从当前工作目录开始。
运行Python脚本时,当前工作目录将设置为执行脚本的目录。
os python模块提供了一种与操作系统交互的可移植方式,该模块是标准Python库的一部分,并包含用于查找和更改当前工作目录的方法。
在Python中获取当前的工作目录
Python中os模块的getcwd()方法返回一个字符串,其中包含当前工作目录的绝对路径,返回的字符串不包含斜杠字符:
os.getcwd()
要使用os模块方法,必须在文件顶部导入模块。
下面是显示如何打印当前工作目录的示例:
# Import the os moduleimport os# Get the current working directorycwd= os.getcwd()# Print the current working directoryprint(Current working directory:{0}.format(cwd))# Print the type of the returned objectprint(os.getcwd() returns an object of type:{0}.format(type(cwd)))
输出将如下所示:
Current working directory:/home/ywnz/Desktop
os.getcwd() returns an object of type:
如果要查找脚本所在的目录,请使用
os.path.realpath(__file__)
它将返回一个字符串,其中包含正在运行的脚本的绝对路径。
在Python中更改当前工作目录
要在Python中更改当前工作目录,请使用chdir()方法:
os.chdir(path)
该方法接受一个参数,即您要更改到的目录的路径,path参数可以是绝对值或相对值。
这是一个例子:
# Import the os moduleimport os# Print the current working directoryprint(Current working directory:{0}.format(os.getcwd()))# Change the current working directoryos.chdir(/tmp)# Print the current working directoryprint(Current working directory:{0}.format(os.getcwd()))
输出将如下所示:
Current working directory:/home/ywnz/Desktop
Current working directory:/tmp
提供给chdir()方法的参数必须是目录,否则会引发NotADirectoryError异常。如果指定的目录不存在,则会引发FileNotFoundError异常。如果运行脚本的用户没有必要的权限,则会引发PermissionError异常:
# Import the os moduleimport ospath=/var/wwwtry: os.chdir(path) print(Current working directory:{0}.format(os.getcwd()))except FileNotFoundError: print(Directory:{0} does not exist.format(path))except NotADirectoryError: print({0} is not a directory.format(path))except PermissionError: print(You do not have permissions to change to{0}.format(path))
结论:要在Python中找到当前的工作目录,请使用os.getcwd(),要更改当前的工作目录,请使用os.chdir(path)。
在Linux里面,创建一个python文件
打开终端,进入要创建Python文件的目录。
创建一个Python文件,例如hello.py,使用vi编辑器:
vi hello.py
在vi编辑器中按下 i进入编辑模式,输入以下代码:
#!/usr/bin/env python3
print("hello world")
这里使用了shebang(#!/usr/bin/env python3)来指定使用Python 3来执行该文件。
按下ESC键,输入:wq保存并退出vi编辑器。
在终端中使用chmod命令修改该文件的权限,使其可执行:
chmod+x hello.py
执行该文件:
python3 hello.py
执行后,终端会输出"hello world"。