Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Python libraries accessible in PYTHONPATH?

I am trying to implement StyleGAN2-ADA PyTorch: https://github.com/NVlabs/stylegan2-ada-pytorch.

On the GitHub repo, it states the following:

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.

What does this mean, and how can I do this?

like image 546
Dino Anastasopoulos Avatar asked Oct 15 '25 05:10

Dino Anastasopoulos


1 Answers

Let's say you have the source code of this repo cloned to /somepath/stylegan2-ada-pytorch which means that the directories you quoted are at /somepath/stylegan2-ada-pytorch/torch_utils and /somepath/stylegan2-ada-pytorch/dnnlib, respectively.

Now let's say you have a python script that you want to access this code. It can be anywhere on your machine, as long as you add this to the top of your python script:

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 )

Note that this only adds those location to PYTHONPATH for the duration of that python script running, so you need this at the top of any python script that intends to use those libraries.

like image 52
Cobra Avatar answered Oct 16 '25 21:10

Cobra