4

I've been trying to figure this out for more than 2 days, screening the internet and the tutorial, but yet I don't have solved my problem. I'm a real newb and don't yet really know what I'm doing..

Software I use: Mac OS X 10.6 Python v3.2.2 Interactive interpreter (IDLE)

Problem: IDLE's default directory is /Users/ME/Documents/. Files with the extention .py can only be opened when located in this directory. However, I made a folder where I would like to save all the .py files etc that have to do with this software. Currently, IDLE cannot load .py files from the chosen directory by me.

What I did first was I added to IDLE: import sys. sys.path.append('Users/Mydir/') sys.path

However, in an already existing thread from 2010 I read sys.path is for the Interpreter ONLY, and that if I am to change this I need to modify the PYTHONPATH environment variable:

PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH" export PYTHONPATH

However, I'm confused how to use this and cannot find answers to my following questions: 1) PYTHONPATH (.py?) is already existing on my computer when I installed the program? If YES, where is it? I cannot find it anywhere. If NO, I need to create one. But where and what should be the content so that IDLE can load files from a non-default directory? Should it contain only the words in bold?

I hope I made my problem clear.

Cheers

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
FlyingDutch
  • 1,100
  • 2
  • 14
  • 24

2 Answers2

7

It's not totally clear to me what you mean by load. That could mean Open and Close files in the IDLE editor. Or it could mean being able to use the Python import statement to load existing Python modules from other files. I'll assume the latter, that by load you mean import.

There are two general ways to launch IDLE on Mac OS X. One is from the command line of a terminal session; if you installed Python 3.2 using the python.org installers, by default typing /usr/local/bin/idle3.2 will work. The other way is by launching IDLE.app from /Applications/Python 3.2, i.e. by double-clicking its icon. Because you say the default directory for files is your Documents folder, I'm assuming you are using the second method because IDLE.app sets Documents as its current working directory, which becomes the default directory for *Open*s and *Save*s and is automatically added as the first directory on Python's sys.path, the list of directories that Python uses to search for modules when importing.

If you want to add other directories to sys.path, as you've noted you can use the PYTHONPATH environment variable to do so. The standard way to do this is to add an export PYTHONPATH=... definition to a shell startup script, like .bash_profile. However, if you use IDLE.app, no shell is involved so commands in .bash_profile have no effect.

While there are ways to modify the environment variables for OS X GUI apps, in this case, a simpler solution is to use the other method to invoke IDLE, from the command line of a shell session, using either /usr/local/bin/idle3.2 or, if you've run the Update Shell Profile command in the /Applications/Python 3.2 folder (and opened a new terminal session), just idle3. Then, a PYTHONPATH environment variable you set up will be inherited by that IDLE.

BTW, there is no direct way to modify the initial current working directory of IDLE.app from Documents other than modifying the code in IDLE. If you start IDLE from a command line, it inherits the current working directory of the shell.

[UPDATE] But rather than fooling around with defining PYTHONPATH, here is another even simpler, and probably better, approach that should work with either IDLE.app or the command line idle. It takes advantage of Python path configuration (.pth) files and user site-package directories. Assuming you are using a standard Python framework build of 3.2 (like from a python.org installer) on Mac OS X, create a path file for the directory you want to permanently add to sys.path. In a terminal session:

mkdir -p ~/Library/Python/3.2/lib/python/site-packages
cd ~/Library/Python/3.2/lib/python/site-packages
cat >my_paths.pth <<EOF
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_1
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_2
EOF

Now, whenever you run that Python 3.2 or IDLE under your user name, the directories you have added to the .pth file will automatically be added to sys.path.

BTW, the exact path location of the user site-packages directory for versions of Python earlier than 3.2 or 2.7 may be slightly different. Also, on other Unix-y systems, the default location for the user site-package directory is ~/.local/lib/python3.2/site-packages.

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • Thank you so much for the clear explanation! I indeed opened IDLE by double-clicking the IDLE.app. I followed your expanation and indeed, when i type 'PYTHONPATH="/ME/Documents/Mydir:$PYTHONPATH" export PYTHONPATH' in the terminal, and after type 'idle3' in the terminal, IDLE launches and I can directly import files from that directory into IDLE. Although I do not succeed in creating a bash profile.When I create a new script and give it the extention .bash_profile, it doesn't launch the terminal. I wrote 1) PYTHONPATH=/.. export PYTHONPATH, and 2) idle3. Idea?TY – FlyingDutch Dec 08 '11 at 20:12
  • It's kind of hard to tell what you are doing. Instead of the `.bash_profile` approach, try creating the `.pth` file as explained in my update. That's a better and simpler approach and will work with either variant of IDLE. I wish I had thought of it initially. – Ned Deily Dec 08 '11 at 20:21
  • Thank you. It looks like this is indeed much easier than the other 2 options you have given. However (and i'm just learning this program), the terminal does not find the directory when i copy your first sentence. My terminal doesn't change to a directory outside the 'My users' domain. I tried both 1) cd ~/Library/Python/3.2/lib/python/site-packages and 2) cd ~/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages (which is the 'true' directory path to site-packages on my mac). Am I missing something here? – FlyingDutch Dec 08 '11 at 20:52
  • It looks like you are confusing the system site-package directory (`/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages`) with the per-user site-package directory (`~/Library/Python/3.2/lib/python/site-packages`). `~` is a shell abbreviation for your home directory so another way to spell the latter is `/Users/your-user-name-here/Library/Python/3.2/lib/python/site-packages`. If, for some reason that directory does not exist, create it with `mkdir -p /Users/your-user-name-here/Library/Python/3.2/lib/python/site-packages` – Ned Deily Dec 08 '11 at 21:14
  • Thank you so much for the help Ned! I can now use IDLE the way I had in mind. The installation of python via python.org did not create the python directory in my user account. Other threads could not help me since they expected this directory to exist with the installation. Thanks to your explanation I created this directory path and when I open the IDDLE.app, it recognizes the newly added directory. This thread may be useful for other people as well who have absolutely no knowledge of programming. Problem solved:D – FlyingDutch Dec 09 '11 at 10:00
  • Glad it was of help. By the way, if you ask a question on StackOverflow, it is expected that you'll either mark an answer as accepted or you will refine your question until you do get an acceptable answer. That's how reputation points are earned. – Ned Deily Dec 09 '11 at 18:10
  • Hm, how do I do this? Maybe I have already done it? Your answer to my question has now a 1 on the left upper corner. Is this proof i accepted your answer? – FlyingDutch Dec 13 '11 at 12:38
0

PYTHONPATH is an environment variable (see here and here). I don't have a Mac, but from the threads I have linked to you would type something like

launchctl setenv PYTHONPATH=/Me/Documents/mydir:$PYTHONPATH

on the command line to allow you to run Python scripts from /Me/Documents/mydir. Alternatively, put this line in a file called .bashrc in your home directory (~) and this path will be set each time each time you open a terminal. See here for a short introduction to .bashrc and other .bash* files. Hope that helps.

EDIT See also this question.

Community
  • 1
  • 1
Chris
  • 44,602
  • 16
  • 137
  • 156
  • Chris, I got an idea of what an environment variable is, but I don't know how to use it. My aim is not to to change OS X variables, just to add a directory in which IDLE can load .py files from. – FlyingDutch Dec 08 '11 at 15:07
  • With the terminal, you mean IDLE? launchctl setenv PYTHONPATH=/Me/Documents/mydir:$PYTHONPATH doesnt work for me. it gives the error 'invalid syntax' – FlyingDutch Dec 08 '11 at 15:25
  • Environment variables are part of your OS, not Python. The PYTHONPATH environment variable must be set before you start the Python IDLE or run a Python script - so don't try and set it from within IDLE. Open a terminal, type `lauchctl setenv PYTHONPATH=/Me/Documents/mydir:$PYTHONPATH`, hit enter and then type Python. You should then be in a Python interpreter which can use scripts from /Me/Documents/mydir. – Chris Dec 08 '11 at 16:08
  • Thanks for helping! I followed your advise and typed the sentence into the terminal, and got this returned: launchctl setenv PYTHONPATH=/ME/Documents/mydir:$PYTHONPATH launchctl usage: setenv Is this to be expected? Have I created a PYTHONPATH variable now? When I boot python in the same terminal, I still have to manually add mydir to the system path (as done in the link you provided). Am I doing something wrong? – FlyingDutch Dec 08 '11 at 19:14
  • You shouldn't be using `launchctl` for this sort of thing. It's the wrong tool for the job. – Ned Deily Dec 08 '11 at 19:36
  • Thank you for replying. Could you explain to me why I shouldn't use launchctl? What should I use then in order to permanently change python IDLE directory? – FlyingDutch Dec 08 '11 at 19:39
  • Even better, refer to my updated answer adding the use of a `.pth` file. – Ned Deily Dec 08 '11 at 20:17