Question

I am somewhat new to Python, having made several scripts, but not too many serious programs. I am trying to understand where to put the functions/scripts (as well as any modules I create in the future) I have written so that they can be accessed by other programs. I've found two different Python help pages on the topic (here and here), which ultimately seem to indicate that files need to be either in the folder (or maybe some sub-folder, I couldn't quite understand the jargon) containing Python executable or in the the current directory. From what I could tell, the default current directory is set with the PYTHONPATH environment variable. However, after setting PYTHONPATH as shown in the screenshot below . . .

Setting PYTHONPATH System Variable

I opened up a new Python shell and checked to see what the current directory was. Below is the output that was produced.

>>> import os
>>> os.getcwd()
'C:\\Program Files\\Python33'
>>>

Could someone please explain what I am doing wrong and how I can make it (if such a thing is possible) so that I have access to any given script I have written which I have placed in some particular folder which I choose to designate my primary working directory (or some sub-folder of such directory)? I do not want to be working from the C:/Program Files/Python33 directory.

If there is any more information needed, I would be happy to provide it. Just let me know.

Was it helpful?

Solution

You seem to have opened interactive mode of python. I am not 100% sure, but I would actually bet my finger that cwd on Windows and *NIXes is by default set to the directory the interpreter was invoked from.

So the question is how did you open your python shell ? Probably from C:\Program Files\Python33 or used some IDE that started it with cwd being the actual direcotry where the python binary resides.

You can pretty much place your files whenever you want, and work relatively from there. However you have to adjust your cwd accordingly. By any means. Usually IDEs provide some project options to set cwd manually. You can run your script from some base directory. E.g.

cd D:
cd D:\my_python_dir\
python test.py

Should work. Also not giving test.py as second argument should start interactive shell and os.getcwd() should give D:/my_python_dir or an equivalent result.

Lastly if you are using interactive shell you can always use other os function os.chdir(path).

OTHER TIPS

You doing nothing wrong. Current working directory C:/Program Files/Python33 and your set PYTHONPATH=D:/Google Drive/Python are two different things.

With your current configuration you can easyly put modules (*.py files) and packages (folders with __init__.py files) inside D:/Google Drive/Python and then import them from any script no matter where from you are running it.

For example, lets assume you've put module mytest.py inside D:/Google Drive/Python. Now you can create script D:/workspace/test.py (no matter where you create it)

import mytest

print(mytest.__file__)

Running it from D:/workspace/ with python test.py will print D:/Google Drive/Python/mytest.py or D:\\Google Drive\\Python\\mytest.py (no windows at hand:().

I think you are messing the two.

PYTHONPATH is the default search path for module files. You can read Environment variables. It does not define your working directory.

And os.getcwd return a string representing the current working directory. It is the directory you open the shell. For example, if I open the shell in /home/hugh, it returns /home/hugh/. If you want to change the current working directory, you can use os.chdir.

If you want to access the module you have written, you can put them in your current working directory or in the PYTHONPATH. If they are in PYTHONPATH, it has nothing to do with your working directory. You can read more about the module search path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top