I am using Python 3.2 on Windows 7. When I open the Python shell, how can I know what the current directory is? How can I change it to another directory (where my modules are)?
-
This has already been discussed [here] [1]: stackoverflow.com/questions/431684/how-do-i-cd-in-pythonmudda– mudda2011-11-23 20:11:23 +00:00Commented Nov 23, 2011 at 20:11
-
5@astay13 -- I think Ignacio means that you aren't intended to change directory to your module-path. You should probably check out the PYTHONPATH environment variable.simon– simon2011-11-23 20:12:29 +00:00Commented Nov 23, 2011 at 20:12
-
1Classical XY problemMaarten Derickx– Maarten Derickx2021-05-21 07:16:37 +00:00Commented May 21, 2021 at 7:16
7 Answers
You can use the os module.
>>> import os
>>> os.getcwd()
'/home/user'
>>> os.chdir("/tmp/")
>>> os.getcwd()
'/tmp'
But if it's about finding other modules: You can set an environment variable called PYTHONPATH, under Linux would be like
export PYTHONPATH=/path/to/my/library:$PYTHONPATH
Then, the interpreter searches also at this place for imported modules. I guess the name would be the same under Windows, but don't know how to change.
edit
Under Windows:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
(taken from http://docs.python.org/using/windows.html)
edit 2
... and even better: use virtualenv and virtualenv_wrapper, this will allow you to create a development environment where you can add module paths as you like (add2virtualenv) without polluting your installation or "normal" working environment.
http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html
5 Comments
sys.path inside of your script.PYTHONPATH globally if you have more than one Python installed (or have programs installed that bundle Python with them -- in other words you'll never know): it may break your installation in mysterious waysyou want
import os
os.getcwd()
os.chdir('..')
3 Comments
os.chdir('C:/Users/Ajeya/Documents'), or os.chdir('C:\\Users\\Ajeya\\Documents'), or os.chdir(r'C:\Users\Ajeya\Documents').os.getcwd() only for debugging purposes so that we can see what the working directory is before we change it. The code to actually change the cwd is just os.chdir('..')>>> import os
>>> os.system('cd c:\mydir')
In fact, os.system() can execute any command that windows command prompt can execute, not just change dir.
3 Comments
system() command changes the cwd only for itself, but not for the parent process. You can check that os.getcwd() will yield the same result before and after any such system() call. Also, to include a backslash \ in a string you have to use \\ or prefix the string with r (for 'raw', viz: r'cd c:\mydir') to avoid that ` \ ` is considered as special (escape) character.The easiest way to change the current working directory in python is using the 'os' package. Below there is an example for windows computer:
# Import the os package
import os
# Confirm the current working directory
os.getcwd()
# Use '\\' while changing the directory
os.chdir("C:\\user\\foldername")
1 Comment
Changing the current directory is not the way to deal with finding modules in Python.
Rather, see the docs for The Module Search Path for how Python finds which module to import.
Here is a relevant bit from Standard Modules section:
The variable sys.path is a list of strings that determines the interpreter’s search path for modules. It is initialized to a default path taken from the environment variable PYTHONPATH, or from a built-in default if PYTHONPATH is not set. You can modify it using standard list operations:
>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')
In answer your original question about getting and setting the current directory:
>>> help(os.getcwd)
getcwd(...)
getcwd() -> path
Return a string representing the current working directory.
>>> help(os.chdir)
chdir(...)
chdir(path)
Change the current working directory to the specified path.
1 Comment
import sys sys.path.append('/home/g/PycharmProjects/your_project/')