472,145 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Very Simple Question: How does Python find modules?

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
4 2034
bartonc
6,596 Expert 4TB
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
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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.

Similar topics

17 posts views Thread by Jan Danielsson | last post: by
73 posts views Thread by Claudio Grondi | last post: by
50 posts views Thread by diffuser78 | last post: by
113 posts views Thread by John Nagle | last post: by
10 posts views Thread by Steven W. Orr | last post: by
27 posts views Thread by Paulo da Silva | last post: by
1 post views Thread by pitjpz | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.