By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,745 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,745 IT Pros & Developers. It's quick & easy.

Very Simple Question: How does Python find modules?

P: 60
I just have one simple question. How do I open a python script from the interactive window? Or do I have to go to File->Open?
Aug 22 '07 #1
Share this Question
Share on Google+
4 Replies

bartonc
Expert 5K+
P: 6,596
I just have one simple question. How do I open a python script from the interactive window? Or do I have to go to File->Open?
To get the names defined in in a script (functions, classes, etc.) into the interactive window, use:
Expand|Select|Wrap|Line Numbers
  1. import scriptname  # leave off the .py extension; must be in the current working directory
If you later modify the script, you must:
Expand|Select|Wrap|Line Numbers
  1. reload scriptname
Aug 22 '07 #2

P: 60
To get the names defined in in a script (functions, classes, etc.) into the interactive window, use:
Expand|Select|Wrap|Line Numbers
  1. import scriptname  # leave off the .py extension; must be in the current working directory
If you later modify the script, you must:
Expand|Select|Wrap|Line Numbers
  1. reload scriptname
What if my scripts are saved in a different folder? Because I get an error that the module does not exist
Aug 24 '07 #3

bartonc
Expert 5K+
P: 6,596
What if my scripts are saved in a different folder? Because I get an error that the module does not exist
3 ways that I know of:

The PYTHONPATH environment variable may be appended to by hand (not a great solution)

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. sys.path.append(r"D:\Python\VideoCapture-0.9-1\Python24") # use the path to your script!
Thats a little better

My favorite:
Put your scripts all in one work folder until the work is complete.
After you have some utility/whatever that you use a lot and it is pretty much finished (probably never 100%), put it in
(on my system) D:\Python24\Lib\site-packages where python will automatically find it. If you want it in its own folder, you'll need a text file with the name of that folder in it. Save it with the .pth extension in the site-packages directory. Eventually, you'll learn to make a package from your work. Packages also live in the site-packages directory.

OK; I guess that's more like 5 ways of showing python where your scripts are.
Aug 24 '07 #4

bartonc
Expert 5K+
P: 6,596
What if my scripts are saved in a different folder? Because I get an error that the module does not exist
Oh, yeah. One more way:

If you open a console (DOS) window and navigate to the folder where your scripts are, then type

python

you'll get an interactive window with the current working directory set correctly.

My favorite console is Console2 because it makes it easy to save sessions and start them back up later.
Aug 24 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.