user1992842
user1992842

Reputation:

PYTHON-changing start up directory

When I start my python shell,it starts in a particular directory. I want to change it to some other directory. So that whenever I open the python shell again in future, it opens in the new set directory.

I am using Python 2.7

Upvotes: 1

Views: 121

Answers (1)

sotapme
sotapme

Reputation: 4903

You could do something like, presuming Unix like system.

Set up PYTHONSTARTUP script to be run when starting Python REPL.

$ export PYTHONSTARTUP=$HOME/.pythonrc

Create python script in $HOME/.pythonrc

# file: ~/.pythonrc
# I always want to start python in my Projects dir.
import os
os.chdir(os.path.join(os.path.expanduser("~"), "Projects"))

Test it in python REPL.

>>> import os
>>> os.getcwd()
'<<YOUR_HOME>>/Projects'

You could change it to suite your specific needs but in general it does the right thing.

Upvotes: 2

Related Questions