gpt4 book ai didi

python - 如何使 Python 库在 PYTHONPATH 中可访问?

转载 作者:行者123 更新时间:2023-12-04 17:12:47 24 4
gpt4 key购买 nike

我正在尝试实现 StyleGAN2-ADA PyTorch:https://github.com/NVlabs/stylegan2-ada-pytorch .

在 GitHub 存储库中,它声明如下:

The above code requires torch_utils and dnnlib to be accessible viaPYTHONPATH. It does not need source code for the networks themselves —their class definitions are loaded from the pickle viatorch_utils.persistence.

这是什么意思,我该怎么做?

最佳答案

假设您将此存储库的源代码克隆到 /somepath/stylegan2-ada-pytorch,这意味着您引用的目录位于 /somepath/stylegan2-ada-pytorch/torch_utils/somepath/stylegan2-ada-pytorch/dnnlib,分别为。

现在假设您有一个要访问此代码的 Python 脚本。它可以在你机器上的任何地方,只要你把它添加到你的 python 脚本的顶部:

import os
import sys

#save the literal filepath to both directories as strings
tu_path = os.path.join('somepath','stylegan2-ada-pytorch','torch_utils')
dnnlib_path = os.path.join('somepath','stylegan2-ada-pytorch','dnnlib')

#add those strings to python path
sys.path.append(tu_path)
sys.path.append(dnnlib_path )

请注意,这只会在该 python 脚本运行期间将这些位置添加到 PYTHONPATH,因此您需要将其放在打算使用这些库的任何 python 脚本的顶部。

关于python - 如何使 Python 库在 PYTHONPATH 中可访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69169145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com