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

importing a user file in Python

P: n/a
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:
>>import odbchelper
params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
print odbchelper.buildConnectionString(params)
server=mpilgrim;uid=sa;database=master;pwd=secret
>>print odbchelper.buildConnectionString.__doc__
Build a connection string from a dictionary
Returns string.
I've tried to enter this into the interpreter but the very first
instruction:
>>>import odbchelper
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:
>>import /home/jw/diveintopython-5.4/py/odbchelper.py
and I get this error:
>>import /home/jw/diveintopython-5.4/py/odbchelper.py
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.
Dec 1 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

On Nov 30, 2007 11:42 PM, waltbrad <wa******@hotmail.comwrote:
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:
>import odbchelper
params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
print odbchelper.buildConnectionString(params)
server=mpilgrim;uid=sa;database=master;pwd=secret
>print odbchelper.buildConnectionString.__doc__
Build a connection string from a dictionary
Returns string.
I've tried to enter this into the interpreter but the very first
instruction:
>>import odbchelper

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:
>import /home/jw/diveintopython-5.4/py/odbchelper.py

and I get this error:
>import /home/jw/diveintopython-5.4/py/odbchelper.py
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.
--
http://mail.python.org/mailman/listinfo/python-list


--
-David
Dec 1 '07 #2

P: n/a
On Dec 1, 1:42 am, waltbrad <waltb...@hotmail.comwrote:
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
Dec 1 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.