Emad Helmi
Emad Helmi

Reputation: 695

Using %cd or % run with environment variables does not work in google colab

I set some environment variables in one cell as below:

# Dataset
%env DATASET_NAME=BOSSbase_1.01
%env DATASET_GDRIVE_PATH=/content/gdrive/My Drive/University/Datasets
%env DATASET_PATH=/content/Datasets

# Result
%env RESULT_PATH=/content/Results
%env RESULT_GDRIVE_PATH=/content/gdrive/My Drive/University/Results

# Code
%env CODE_PATH=/content/Code

# Run
%env EMBED_RATIO=0.8
%env EMBED_ALGORITHM=wow
%env MODEL_NAME=BOSSbase_7030_810

When I use ! echo $VARIABLE_NAME in another cell I get the desired result and the content of the variable echoed correctly. But when in one cell I use %cd $VARIABLE_NAME or for running a python code with %run I get this error message:

[Errno 2] No such file or directory: '$CODE_PATH'

How can I use cd command in a cell with an environment variable?

Upvotes: 0

Views: 830

Answers (2)

It's an old question but without good answer. This works better and it's easier:

YOUR_VARIABLE = "/XXXX/XXXX/"

%cd {YOUR_VARIABLE}

Enjoy!

Upvotes: 1

korakot
korakot

Reputation: 40888

If you cannot to that, you can choose the longer version.

import os
path = os.environ['CODE_PATH']
os.chdir(path)

Upvotes: 1

Related Questions