Colab中将路径添加到PYTHONPATH

Colab中将路径添加到PYTHONPATH

本文介绍了如何在Google Colab中将路径添加到PYTHONPATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在google colab中执行以下命令. export PYTHONPATH =/project/pylib/src:$ PYTHONPATH

how to execute the following command in google colab.export PYTHONPATH=/project/pylib/src:$PYTHONPATH

!export PYTHONPATH =/project/pylib/src:$ PYTHONPATH 不受影响.

推荐答案

编辑2020-11-12

使用%"代替!"会保留会话中所有单元格的任何更改.因此,我们可以使用它来设置环境变量"PYTHONPATH".但是,导出不适用于colab.%env可以用来设置环境变量.

Edit 2020-11-12

Using `%` instead of `!` retains any changes to all cells in the session. So, we can use that to set the environment variable `PYTHONPATH`. However, export doesn't work in colab. `%env` can be used to set the environment variable.

! echo $PYTHONPATH
%env PYTHONPATH="$/env/python:/content/gdrive/My Drive/Colab Notebooks/MNIST_Classifier/src"
! echo $PYTHONPATH

输出:

/env/python
/env/python:/content/gdrive/My Drive/Colab Notebooks/MNIST_Classifier/src

初始答案

以下为我工作

! echo $PYTHONPATH

import os
os.environ['PYTHONPATH'] += ":/content/gdrive/My Drive/Colab Notebooks/MNIST_Classifier/src"

! echo $PYTHONPATH

输出:

/env/python
/env/python:/content/gdrive/My Drive/Colab Notebooks/MNIST_Classifier/src

资料来源:
https://medium.com/@omernaeem/您可以使用os-environ-78a5181b6376设置环境变量
https://stackoverflow.com/a/49684719/3337089

这篇关于如何在Google Colab中将路径添加到PYTHONPATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:12