473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2085
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

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

Similar topics

17
by: Jan Danielsson | last post by:
Hello all, I recently started using Python, and I must say I like it. Both the language and libraries available for it. Background: I have written an application which I use to keep track of...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
1
by: Mark Asbach | last post by:
Hi pythonians, I'm one of the maintainers of an open source image processing toolkit (OpenCV) and responsible for parts of the autotools setup. The package mainly consists of four shared...
113
by: John Nagle | last post by:
The major complaint I have about Python is that the packages which connect it to other software components all seem to have serious problems. As long as you don't need to talk to anything outside...
10
by: Steven W. Orr | last post by:
I saw this and tried to use it: ------------------><8------------------- const.py------------- class _const: class ConstError(TypeError): pass def __setattr__(self,name,value): if...
27
by: Paulo da Silva | last post by:
Hi! I was told in this NG that string is obsolet. I should use str methods. So, how do I join a list of strings delimited by a given char, let's say ','? Old way:
1
by: pitjpz | last post by:
We have moved our Database to another server. The server it was on used SQL 4 and the new one its on now uses SQL5 the only problem we can find is that when you attempt to delete a record from...
0
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.