python package confusion
Lipska the Kat
lipska at lipskathekat.com
Mon Jul 23 06:02:58 EDT 2012
Hello again pythoners
I'm trying to understand the python package stuff
I have the following directory
/home/lipska/python/dev/mods
In this directory I have two files, both executable
----------------------
#! /usr/bin/env python3.2
# fibo.py Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
----------------------
#! /usr/bin/env python3.2
# test.py fibo module test program
import fibo
fibo.fib(1000)
-----------------------
The PYTHONPATH ev is set to /home/lipska/python/dev/mods:.
in .bashrc
The following interactive session works thusly
lipska at ubuntu:~/python/dev/mods$ python3.2
Python 3.2.3 (default, Jul 17 2012, 14:23:10)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fibo
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>>
However if I try and run test.py as an executable I get the following error
lipska at ubuntu:~/python/dev/mods$ test.py
Traceback (most recent call last):
File "./test.py", line 5, in <module>
fib(1000)
NameError: name 'fib' is not defined
if I print sys.path I get the following
>>> print(sys.path)
['', '/usr/local/lib/python32.zip', '/usr/local/lib/python3.2',
'/usr/local/lib/python3.2/plat-linux2',
'/usr/local/lib/python3.2/lib-dynload',
'/home/lipska/.local/lib/python3.2/site-packages',
'/usr/local/lib/python3.2/site-packages']
The documentation states that ...
sys.path is initialized from these locations:
the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the
shell variable PATH).
But apparently the additional locations I specify in .bashrc are not
being added to sys.path
I also have an empty file __init__.py in the mods directory
Not sure what I'm doing wrong here
Any help much appreciated.
Lipska
--
Lipska the Kat: Troll hunter, Sandbox destroyer
and Farscape dreamer of Aeryn Sun.
More information about the Python-list
mailing list