Ubuntu 配置Python环境(包括Tensorflow)

本文详细介绍了在Ubuntu系统中配置Python环境,包括安装miniconda、Spyder,创建conda虚拟环境,以及如何管理包。重点讲解了Python版本升级、CUDA和cuDNN的安装,以及TensorFlow的配置。此外,还讨论了conda环境的导出与导入,解决conda安装yaml环境时可能遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1,安装miniconda

2,安装Spyder 

3,为什么要创建conda的虚拟环境

创建python任意版本虚拟环境:

4,pip 和 conda 包管理器

5,升级更新python版本

6,利用conda查找可安装的包版本

7,TensorFlow 安装与环境配置


1,安装miniconda

https://gist.github.com/arose13/fcc1d2d5ad67503ba9842ea64f6bac35

How to Install miniconda on linux (from the command line only)

# Setup Ubuntu
sudo apt update --yes
sudo apt upgrade --yes

# Get Miniconda and make it the main Python interpreter
#-O:下载并以指定的文件名保存
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh 
bash ~/miniconda.sh -b -u -p ~/miniconda
rm ~/miniconda.sh

export PATH=~/miniconda/bin:$PATH

#
#The miniconda.sh script comes with a few basic options. 
#Most notably we used -b to be able to run unattended, which means that all of the agreements are automatically accepted without user prompt.
# -u updates any existing installation in the directory of install if there is one.
# -p is the directory to install into and defaults to /root/miniconda3 .

usage: /root/miniconda3/miniconda.sh [options]

Installs Miniconda3 4.6.14

-b           run install in batch mode (without manual intervention),
             it is expected the license terms are agreed upon
-f           no error if install prefix already exists
-h           print this help message and exit
-p PREFIX    install prefix, defaults to /root/miniconda3, must not contain spaces.
-s           skip running pre/post-link/install scripts
-u           update an existing installation
-t           run package tests after installation (may install conda-build)

或者: 从官方网站Miniconda — Conda documentation下载相应的 Miniconda sh 文件,然后使用 sh <FILENAME> -b 从命令行执行安装

cd Downloads/
sh Miniconda3-py39_4.10.3-Linux-x86_64 -b  
#-b          run install in batch mode (without manual intervention),
#             it is expected the license terms are agreed upon
export PATH=/home/kevalen/miniconda3/bin:$PATH
################################################
For best results, please verify that your PYTHONPATH only points to
    directories of packages that are compatible with the Python interpreter
    in Miniconda3: /home/kevalen/miniconda3

检查是否安装成功:

输入$ conda,如果报错conda: command not found

原因是因为~/.bashrc文件没有配置好

vim ~/.bashrc
在.bashrc最后一行加上 export PATH=/home/kevalen/miniconda3/bin:$PATH
或者 export PATH=/home/lei/miniconda/bin:$PATH
然后保存更改,运行
source ~/.bashrc

打开终端报错:bash: /某路径/bashrc: No such file or directory

造成这样的原因,一般是 bashrc 文件里的环境变量配置出了问题。只要删除对应“某路径”的source那一行即可解决,比如

bash: workspace_dir: No such file or directory

删除 source workspace_dir/devel/setup.bash这一行就可以啦

查看conda环境:

conda info --env
conda env list

2,安装Spyder 

Installation Guide — Spyder 5 documentation

ubuntu 20 安装 spyder3_ycs_0405的专栏-CSDN博客

1、先安装cython:   

python -m pip install cython
2、安装spyder3  

sudo apt install spyder3
或者用下面的命令安装:

sudo apt install spyder
3、启动spyder3  

    终端输入:

spyder
或

spyder3

########################################
如果安装了miniconda,则可以直接通过 conda包管理器 安装:
conda install spyder

移除一个包
例如:移除 spyder

conda remove spyder

更新一个包
例如:更新 spyder

conda update spyder

查看安装是否成功:
$ spyder

####################################也可以使用 Python 包管理器 pip 安装
pip install spyder

#更新
pip install --upgrade spyder

#pip卸载包
命令:pip uninstall packagename

#pip查看已安装的包
#功能:查看指定的安装包信息
命令:pip show packagename

#功能:列出所有的安装包
命令:pip list 


卸载Spyder:

https://www.thelinuxfaq.com/ubuntu/ubuntu-17-04-zesty-zapus/spyder?type=uninstall

#Only uninstall or removes an installed spyder package itself
sudo apt-get remove spyder 
#Uninstall spyder including dependent package
sudo apt-get remove --auto-remove spyder 
#If you use purge options along with auto remove, will be removed everything
#regarding the package, It's really useful when you want to reinstall again.
sudo apt-get purge --auto-remove spyder

解决Spyder不能使用plot_model 

keras - ImportError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work - Stack Overflow

#I solved this problem by installing :

conda install graphviz
conda install pydot
conda install pydotplus

#PS: i called plot_model with: and it's working now.

from tensorflow.keras.utils import plot_model

3,为什么要创建conda的虚拟环境

为什么要创建anaconda的虚拟环境_ice bear桑的博客-CSDN博客_anaconda为什么要创建环境

在 Python 开发中,很多时候我们希望每个应用有一个独立的 Python 环境(比如应用 1 需要用到 TensorFlow 1.X,而应用 2 使用 TensorFlow 2.0)。这时,Conda 虚拟环境即可为一个应用创建一套 “隔离” 的 Python 运行环境。使用 Python 的包管理器 conda 即可轻松地创建 Conda 虚拟环境。常用命令如下:

conda create --name [env-name]      # 建立名为[env-name]的Conda虚拟环境
conda activate [env-name]           # 进入名为[env-name]的Conda虚拟环境
conda deactivate                    # 退出当前的Conda虚拟环境
conda env remove --name [env-name]  # 删除名为[env-name]的Conda虚拟环境
conda env list                      # 列出所有Conda虚拟环境

Anaconda多环境多版本python配置指导 - 知乎

conda create --name [env-name] spyder  #在[env-name]环境中安装spyder

#很多跟在--后边常用的命令选项,可以被略写为一个短线-加命令首字母。所以--name选项和-n的作用是一样的

#激活这个新环境
Linux,OS X: source activate [env-name]
Windows:    activate [env-name]

#可以创建环境bunnies时同时安装多个包,例如这三个包:python=3,Astroid,Babel
co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值