Sorry, you can't do that with a program that's launched in the normal way. At least, you can't in a POSIX-compliant OS.
When you run a script or program in the normal way it gets run in a new process, and of course any changes made in that new process don't affect the parent process: child processes inherit environment from their parent, the parent's environment can't by affected by any changes the child makes to its environment.
There's kind of a way around this: you can put a cd
command into a script and then source
that script, which executes the script in the current process rather than running it in a new process. I guess your Python code could create a tiny shell script that changes to the desired directory, but you will still need the user to source
that script to make the actual directory change. When I need to do this for my own use, I just print the desired cd
command to the shell so I can easily copy & paste it and then hit Enter. :)
Take a look here for more details about why cd
is like this.