Jonathan Tse
Jonathan Tse

Reputation: 995

How to %cd to a path containing env variable in jupyter notebook

I am setting a project path to some env variable like PROJECT_HOME by us using os.environ like this:

os.environ['PROJECT_HOME'] = os.getcwd()

so I can use %cd $PROJECT_HOME/abc/xyz in later cell

However, the system return this:

[Errno 2] No such file or directory: '${PROJECT_HOME}/abc/xyz'

Is there a way to use env variable in %cd?

Upvotes: 1

Views: 1000

Answers (2)

Jonathan Tse
Jonathan Tse

Reputation: 995

I just realized the magic %cd could access python variable. Therefore it could be done like this:

import os
PROJECT_HOME = os.getcwd()

%cd {PROJECT_HOME}

Having said that, the answer by Daniel is the right answer to the question because I asked how to %cd a path containing an environment variable.

Upvotes: 2

Daniel Schneider
Daniel Schneider

Reputation: 2046

This works for me: %cd {os.environ['PROJECT_HOME']}

Upvotes: 3

Related Questions