[Tutor] How does one construct a module for import?

Steven D'Aprano steve at pearwood.info
Mon Jun 2 18:47:25 CEST 2014


On Mon, Jun 02, 2014 at 05:08:21PM +0100, Sydney Shall wrote:
> Thanks for the detailed reply, Steven.
> It seems that you have correctly identified my prblem.
> 
> But I am still puzzled, because I do not know how this happened.
> I simply copied the two files that I wished to import to a directory 
> called (nowMyModule).
> It now contains only three files;
> >
> pwd
> Out[99]: 
> u'/Users/sydney/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/MyModule'
> 
> ls -l
> total 32
> -rw-r--r--  1 sydney  staff    29 Jun  2 16:31 __init__.py
> -rw-r--r--  1 sydney  staff  2936 Jun  2 16:32 findGraphParametersV2.py
> -rw-r--r--  1 sydney  staff  7147 Jun  2 16:32 plotDataV2.py
> 
> What does the line above; total 32 refer to?

It is the number of disk blocks used by the files, assuming 1024 bytes 
per block. In this example, the __init__.py file uses 29 bytes, and so 
requires a minimum of 1 block; the findGraph file uses 2936 bytes, so 
requires at least 3 blocks; the plotData file uses 7147 bytes, so it 
needs at least 7 blocks. I don't actually know where the remaining 21 
blocks end up. Possibly in the directory itself?

> But when I do the following, this is what I get.
> 
> 
> dir(plotDataV2)
> Out[107]:
> ['__builtins__',
>  '__doc__',
>  '__file__',
>  '__name__',
>  '__package__',
>  'plotData',
>  'pylab']
> 
> 
> In the file plotDataV2, I have imported of course, pylab.
> I do not know where plotData comes from although there is such a file in 
> another directory.

You will have a line like

    from findGraphParametersV2 import plotData

or possibly:

    plotData = 23  # or anything really, who knows?

or 

    def plotData(): ...


There's no way for us to tell what plotData is. But YOU can do it:

print(plotDataV2.plotData)



If that doesn't display enough information for you to identify what it 
is, try these two commands:

print(repr(plotDataV2.plotData))
print(type(plotDataV2.plotData))



> dir(findGraphParametersV2)
> Out[108]:
> ['__builtins__',
>  '__doc__',
>  '__file__',
>  '__name__',
>  '__package__',
>  'findGraphParameters',
>  'np',
>  'pylab']
> 
> Here I have imported numpy as np, pylab and math which doers not appear?

If math does not appear, you haven't imported it. Possibly you imported 
it in findGraphParametersV1 or findGraphParametersV3, but not V2.

Or maybe you imported it, but then deleted it:

import math
del math

Again, there is no way for us to tell what you've done just by the 
result. 

 
> I do understand that I should have only ONE directory containing my own 
> Modules and only ONE copy of each file in that directory. But that is 
> what I thought I had done.

Um, yes?


> Do I undestand correctly that what I need to do is to have a single 
> directory, say called MyModule, in the directory ....site-packages. And 
> then I need to copy just once each of the two function files that I want 
> to be importable?

No, I'm saying, *forget about directories of files*. Have ONE FILE per 
project. You seem to be confusing yourself into all sorts of knots by 
having multiple files trying to import other files. You're working with 
advanced functionality, packages, before you understand how to deal with 
basic functionality, modules and attributes.


You currently have this:

site-packages/                       # directory
+--  MyModule                        # directory inside site-packages
.....+-- __init__.py                 # file inside MyModule
.....+-- findGraphParametersV2.py    # another file
.....+-- plotDataV2.py               # yet another file


Instead, I suggest you should have:

site-packages/                       # directory
+--  MyModule.py                     # file inside site-packages


and put the contents of findGraphParametersV2.py and plotDataV2.py 
inside the MyModule.py file.



-- 
Steven


More information about the Tutor mailing list