我正在尝试实现 StyleGAN2-ADA PyTorch:https://github.com/NVlabs/stylegan2-ada-pytorch .
在 GitHub 存储库中,它声明如下:
The above code requires torch_utils and dnnlib to be accessible via PYTHONPATH. It does not need source code for the networks themselves — their class definitions are loaded from the pickle via torch_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/