0

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.

tlewis3348
  • 424
  • 2
  • 7
  • 21
  • `PYTHONPATH` is where the Python interpreter will look for modules it doesn't find in the current directory. For starting up the shell, you can put the path to a startup script to an env var named `PYTHONSTARTUP` which will be executed when you launch the Python shell. You ought to be able to make this script prompt you for what the current working directory should be and change to it. Alternatively, there could be some separate step that you do that sets what directory you want changed to every time you start the shell which is saved somewhere and then used by your custom startup script. – martineau Jul 18 '13 at 16:10

3 Answers3

1

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).

luk32
  • 15,812
  • 38
  • 62
  • FYI: `cd` does not work the change to a drive other than the C-drive. To accomplish this, enter just `D:`. Once in that drive, `cd` works as expected. – tlewis3348 Jul 18 '13 at 16:45
  • Once I am in the interactive mode of Python, how would I execute one of my scripts? Using your example, how would I execute `test.py` from within the interactive mode of Python? – tlewis3348 Jul 18 '13 at 17:16
1

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:().

twil
  • 6,032
  • 1
  • 30
  • 28
0

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.

zhangyangyu
  • 8,520
  • 2
  • 33
  • 43