假设我在我的torch帐户中使用pip3安装了user1。
user1@desktop:~$ pip3 show torch
Name: torch
Version: 1.11.0
Summary: Tensors and Dynamic neural networks in Python with strong GPU acceleration
Home-page: https://pytorch.org/
Author: PyTorch Team
Author-email: packages@pytorch.org
License: BSD-3
Location: /home/user1/.local/lib/python3.8/site-packages
Requires: typing-extensions
Required-by: ogb, pytorch-lightning, torchaudio, torchmetrics, torchvision如上文所示,pip3-installed包的位置是/home/user1/.local/lib/python3.8/site-packages。
现在,假设我想使用sudo特权运行一些python文件。(假设我可以在使用sudo特权时传递正确的密码。)但是,在这种情况下,它返回ModuleNotFoundError,因为torch安装在我的user1帐户中,而不是根用户的帐户中。
user1@desktop:~$ sudo python3 toy_example.py
Traceback (most recent call last):
File "toy_example.py", line 1, in <module>
import torch
ModuleNotFoundError: No module named 'torch'当我试图使用sudo权限运行某些文件时,如何链接安装在普通用户帐户中的python包?(例如,torch,在上面的示例中安装在/home/user1/.local/lib/python3.8/site-packages中)我认为如果修复一些环境变量是可能的,但无法解决。
发布于 2022-07-08 08:42:36
@minolee的评论解决了这个问题。下面的命令对我来说很好。
sudo PYTHONPATH=/home/user1/.local/lib/python3.8/site-packages python3 toy_example.py访问Python - add PYTHONPATH during command line module run获得更多帮助。
https://stackoverflow.com/questions/72907581
复制相似问题