importing a user file in Python

W

waltbrad

Hello. I'm brand new to Python. I'm running a Kubuntu system and
working through the "diveintopython" tutorial. The author of that
gives these instructions:
Build a connection string from a dictionary
Returns string.


I've tried to enter this into the interpreter but the very first
instruction:

gives me this:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named odbche

I've tried to enter the pathname:

and I get this error:
File "<stdin>", line 1
import /home/jw/diveintopython-5.4/py/odbchelper.py
^
SyntaxError: invalid syntax

Where on my system do I have to place these files before the
interpreter will import them?

Thanks.
 
D

David Tweet

You have options:
1) Have the file in your current working directory, in which case
it's just "import odbchelper".
2) Change your PYTHONPATH in your shell, adding a line like this to
your bashrc perhaps:
export PYTHONPATH=$PYTHONPATH:/home/jw/diveintopython-5.4/py
... and do the same import line in your code once you've
sourced your bashrc again.
 
R

Rick Dooling

Hello. I'm brand new to Python.

Where on my system do I have to place these files before the
interpreter will import them?

In this case, odbchelper.py is a module you are trying to import.

http://docs.python.org/tut/node8.html

One suggestion is to make a directory in your /home dir where you keep
your downloaded and homemade Python modules and scripts. I call mine
"Python" so it's ~/Python or /home/rick/Python.

Then, as suggested, add this dir to your Python path, so that you can
import your custom modules (with import odbchelper).

You may also want to add that dir to your system path, so that you can
call your Python scripts from anywhere without having to change back
to the ~/Python directory:

export PATH=~/Python:~/bin:$PATH

Here I've added two directories: Python and bin to $Path so that the
system knows to look for executables there.

Good luck.

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top