ma@ubuntu:~/Downloads/torchvision$ python3 setup.py install setup.py:10: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html from pkg_resources import DistributionNotFound, get_distribution, parse_version Building wheel torchvision-0.15.1 Compiling extensions with following flags: FORCE_CUDA: False DEBUG: False TORCHVISION_USE_PNG: True TORCHVISION_USE_JPEG: True TORCHVISION_USE_NVJPEG: True TORCHVISION_USE_FFMPEG: True TORCHVISION_USE_VIDEO_CODEC: True NVCC_FLAGS: Found PNG library Building torchvision with PNG image support libpng version: 1.6.37 libpng include path: /usr/include/libpng16 Running build on conda-build: False Running build on conda: False Building torchvision with JPEG image support Building torchvision without NVJPEG image support ffmpeg: symbol lookup error: ffmpeg: undefined symbol: avio_protocol_get_class, version LIBAVFORMAT_58 Building torchvision without ffmpeg support Error fetching ffmpeg version, ignoring ffmpeg. Building torchvision without ffmpeg support Building torchvision without video codec support running install /home/ma/.local/lib/python3.8/site-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated. !! ******************************************************************************** Please avoid running ``setup.py`` directly. Instead, use pypa/build, pypa/installer or other standards-based tools. See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details. ******************************************************************************** !! self.initialize_options() /home/ma/.local/lib/python3.8/site-packages/setuptools/_distutils/cmd.py:66: EasyInstallDeprecationWarning: easy_install command is deprecated. !! ******************************************************************************** Please avoid running ``setup.py`` and ``easy_install``. Instead, use pypa/build, pypa/installer or other standards-based tools. See https://github.com/pypa/setuptools/issues/917 for details. ******************************************************************************** !! self.initialize_options() error: can't create or remove files in install directory The following error occurred while trying to add or remove files in the installation directory: [Errno 13] Permission denied: '/usr/lib/python3.8/site-packages' The installation directory you specified (via --install-dir, --prefix, or the distutils default setting) was: /usr/lib/python3.8/site-packages/ This directory does not currently exist. Please create it and try again, or choose a different installation directory (using the -d or --install-dir option).
时间: 2025-06-03 22:59:29 浏览: 26
<think>我们正在解决两个问题:1.在运行`pythonsetup.pyinstall`安装torchvision时遇到权限问题(Permissiondenied)2.安装过程中缺少依赖(libpng,ffmpeg)导致的错误解决方案:一、解决权限问题当我们使用`pythonsetup.pyinstall`时,它默认会尝试将包安装到系统Python的site-packages目录(例如`/usr/local/lib/python3.X/site-packages`),这需要root权限。如果没有使用sudo,就会遇到Permissiondenied错误。有两种方法可以解决权限问题:方法1:使用sudo(不推荐,因为可能会影响系统Python环境)```bashsudopythonsetup.pyinstall```方法2:推荐使用虚拟环境(virtualenvironment)或者用户安装(--user)选项,这样就不需要root权限。使用虚拟环境(推荐):```bash#创建虚拟环境python-mvenvmyenvsourcemyenv/bin/activate#Linux/Mac#或者Windows:myenv\Scripts\activate#在虚拟环境中安装torchvisionpythonsetup.pyinstall```或者使用`--user`选项安装到用户目录:```bashpythonsetup.pyinstall--user```二、解决依赖问题(libpng,ffmpeg等)在编译torchvision时,需要确保系统已经安装了必要的开发库。这些库包括:-libjpeg-dev-libpng-dev-ffmpeg(用于视频支持)安装这些依赖的方法因操作系统而异。对于Ubuntu/Debian系统:```bashsudoapt-getupdatesudoapt-getinstalllibjpeg-devlibpng-devlibavcodec-devlibavformat-devlibswscale-dev#如果需要ffmpeg,可以安装sudoapt-getinstallffmpeg```对于CentOS/RHEL系统:```bashsudoyuminstalllibjpeg-devellibpng-develsudoyuminstallffmpegffmpeg-devel```对于macOS(使用Homebrew):```bashbrewinstalljpeglibpngffmpeg```安装完这些依赖后,重新运行安装命令。三、重新安装torchvision确保在正确解决了权限问题和依赖问题后,重新安装:在虚拟环境中(或使用--user):```bashpythonsetup.pyinstall```或者使用pip安装(如果你不想从源码安装,也可以直接使用pip安装预编译的版本,但有时预编译版本可能不包含某些功能):```bashpipinstalltorchvision```四、验证安装安装完成后,在Python中尝试导入torchvision并检查图像操作是否正常:```pythonimporttorchvisionfromtorchvision.ioimportread_image#尝试读取一张图片(需要有一张测试图片,比如'test.jpg')img=read_image('test.jpg')print(img.shape)```如果没有报错,说明安装成功。注意:有时torchvision的版本需要和PyTorch匹配,请确保安装的torchvision版本与已安装的PyTorch版本兼容。可以在torchvision的GitHub仓库的releasenotes中查看对应关系。如果安装后仍然出现关于libjpeg/libpng的警告,可能是因为在编译时没有正确链接到这些库。可以尝试设置环境变量,指定库的路径,例如:```bashexportPKG_CONFIG_PATH="/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH"```然后再重新运行安装命令。五、关于ffmpeg的说明如果视频功能不是必需的,可以在编译torchvision时禁用视频支持,这样可以跳过ffmpeg的依赖。在运行setup.py时添加环境变量:```bashTORCHVISION_USE_FFMPEG=0pythonsetup.pyinstall```但是,如果视频功能是必需的,请确保ffmpeg及其开发库已经安装。总结步骤:1.创建虚拟环境(推荐,避免权限问题)2.安装系统依赖(libjpeg,libpng,ffmpeg等)3.设置环境变量(如果需要,确保编译器能够找到依赖库)4.运行安装命令(使用虚拟环境,不需要sudo)如果问题仍然存在,请提供具体的错误信息以便进一步分析。---§§相关问题§§1.如何检查系统中是否已安装`libjpeg`和`libpng`?2.在Windows系统上如何解决`torchvision`的安装权限和依赖问题?3.安装`torchvision`时如何选择与PyTorch版本兼容的版本?4.如何查看`torchvision`编译时的选项(如是否启用了ffmpeg)?5.在Docker容器中安装`torchvision`需要注意哪些依赖问题?</think>
### 解决 Torchvision 安装中的权限和依赖问题
您遇到的权限问题(`Permission denied`)和依赖问题(`libpng`, `ffmpeg`错误)是 Torchvision 编译安装时的常见问题。以下是系统的解决方案:
#### 一、解决权限问题
1. **使用虚拟环境安装**(推荐方案)
创建独立的 Python 环境避免系统权限问题:
```bash
python -m venv torchvision-env # 创建虚拟环境
source torchvision-env/bin/activate # 激活环境(Linux/Mac)
# Windows: torchvision-env\Scripts\activate
pip install --upgrade pip setuptools wheel
pip install torchvision --no-cache-dir # 优先尝试安装二进制包
```
2. **使用用户级安装选项**
不需要 sudo 权限:
```bash
pip install --user torchvision # 安装到用户目录
```
3. **修复目录权限**
如果您坚持从源码编译:
```bash
sudo chown -R $USER /usr/local # Linux/Mac
# 或者指定安装位置
python setup.py install --prefix ~/.local # 用户目录
```
#### 二、解决依赖问题(libpng, ffmpeg等)
1. **安装编译依赖库**:
```bash
# Ubuntu/Debian
sudo apt-get install libjpeg-dev libpng-dev ffmpeg libavcodec-dev libavformat-dev libswscale-dev
# CentOS/RHEL
sudo yum install libjpeg-devel libpng-devel ffmpeg ffmpeg-devel
# macOS (使用Homebrew)
brew install jpeg libpng ffmpeg pkg-config
```
2. **设置环境变量**:
确保编译器能找到依赖库:
```bash
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH"
```
3. **强制启用视频支持**(解决 ffmpeg 错误):
```bash
# 从源码安装时指定
TORCHVISION_USE_FFMPEG=1 python setup.py install
```
4. **使用 conda 管理依赖**(解决库冲突):
```bash
conda create -n torchvision-env python=3.8
conda activate torchvision-env
conda install pytorch torchvision cudatoolkit=11.3 -c pytorch
```
#### 三、完整解决流程
1. **创建虚拟环境**
```bash
python -m venv tv-env && source tv-env/bin/activate
```
2. **安装系统依赖**
```bash
sudo apt-get install -y libjpeg-dev libpng-dev ffmpeg
```
3. **配置编译环境**
```bash
export C_INCLUDE_PATH="/usr/local/include:$C_INCLUDE_PATH"
export LIBRARY_PATH="/usr/local/lib:$LIBRARY_PATH"
```
4. **安装 Torchvision**
```bash
pip install --no-cache-dir torchvision # 首选二进制安装
```
如果失败,从源码编译:
```bash
git clone https://github.com/pytorch/vision.git
cd vision
TORCHVISION_USE_FFMPEG=1 python setup.py install
```
#### 四、验证安装
```python
from torchvision.io import read_image, read_video
# 测试图像支持
img = read_image("test.jpg")
print(f"Image loaded: {img.shape}")
# 测试视频支持
video, _, _ = read_video("test.mp4", pts_unit='sec')
print(f"Video loaded: {video.shape}")
```
> **重要提示**:如果仍然报错,尝试使用 Docker 官方镜像避免环境问题:
> ```bash
> docker run -it pytorch/pytorch:latest bash
> pip install torchvision
> ```
#### 五、疑难排查
1. **定位缺失依赖**:
```bash
ldd build/lib.linux-x86_64-3.8/torchvision/_C.so | grep "not found"
```
2. **查看详细编译日志**:
```bash
python setup.py install --verbose 2>&1 | tee build.log
```
3. **降级版本匹配PyTorch**:
```python
# 参考版本兼容表 https://github.com/pytorch/vision#installation
pip install torchvision==0.11.1+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
```
---
阅读全文
相关推荐



















