Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25844

python package confusion

Date 2012-07-23 11:02 +0100
From Lipska the Kat <lipska@lipskathekat.com>
Newsgroups comp.lang.python
Subject python package confusion
Message-ID <BsCdnb3NJ_bOvJDNnZ2dnUVZ7rmdnZ2d@bt.com> (permalink)

Show all headers | View raw


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@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@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.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

python package confusion Lipska the Kat <lipska@lipskathekat.com> - 2012-07-23 11:02 +0100
  Re: python package confusion Dave Angel <d@davea.name> - 2012-07-23 06:19 -0400
    Re: python package confusion Lipska the Kat <lipska@lipskathekat.com> - 2012-07-23 11:54 +0100
  Re: python package confusion Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-07-23 06:22 -0400
    Re: python package confusion Lipska the Kat <lipska@lipskathekat.com> - 2012-07-23 12:02 +0100
      Re: python package confusion David <bouncingcats@gmail.com> - 2012-07-23 21:16 +1000
        Re: python package confusion Lipska the Kat <lipska@lipskathekat.com> - 2012-07-23 12:43 +0100
    Re: python package confusion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-24 05:38 +0000
      Re: python package confusion Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-07-24 03:49 -0400

csiph-web