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?

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.

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?

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.

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?

Once again, many thanks for your advice,
Sydney


On 02/06/2014 16:31, Steven D'Aprano wrote:
On Mon, Jun 02, 2014 at 01:21:29PM +0100, Sydney Shall wrote:
I am having a similar problem.
Actually, I don't think so. Your problem doesn't appear to have anything
to do with the problem that Charles Agriesti is having. The only
connection seems to be that you are both using Python. Read on for more
details.


I have now worked out how to copy my helper file to the correct
location, in my case is:
'/Users/sydney/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages'

When I type the following at the IPython prompt I get no error message;
import findGraphParametersV2
Right. This says that, unlike Charles' situation, in your case Python is
correctly importing your module. You may have a problem, but *importing*
is not the problem.



And the following led me to believe all was well.

I type in the following:
In [19]: dir(findGraphParametersV2)

Out[19]:
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
  'findGraphParameters', 'np', 'pylab']
This shows that the findGraphParametersV2 (whew, that's a mouthful!)
module has eight attributes. Some of them (like __name__) are created
automatically by Python. Others, like np and pylab, are probably created
when your module imports other modules. The one that you probably care
about is findGraphParameters, which you need to call using:

     findGraphParametersV2.findGraphParameters( arguments )

Notice that you need to give the module name first, followed by the name
of the thing inside the module.

However, when I use the import statement in my program I get a runtime
error as follows:
Just a moment. A couple of sentences ago, you said that importing works.
Now you say it doesn't. Which is it?

Please be more specific about the code you are running. Unfortunately,
while we know Python quite well, we're not very good at reading your
mind, and we can't see your code. You need to identify what line of code
is being run, and tell us.

If the code is:

     import findGraphParametersV2

which then fails with ImportError, that tells us some valuable
information. If the code is:

     result = findGraphParametersV2(some, arguments, here)

which then fails with the error you mentioned:

     TypeError: 'module' object is not callable

that tells us something completely different! Both the type of the
exception (ImportError, TypeError) and the error message are important,
but equally important is what you did that resulted in the error.


<ipython-input-14-abb1b897e8b9> in <module>()
----> 1 CapitalSimulation(51, 4000.0, 20.0, 20.0, 100, 1.0, 0.0, 40.0, 1.0)

/Users/sydney/My_Documents/Political_Economy/Capital_Simulation/Capital/Current
version/CapitalWithProdV14.py in CapitalSimulation(number_of_cycles,
capital_advanced, unit_constant_capital, wagerate, labour_powers,
productivity, prodinc, work_duration, labour_intensity)

If I am reading this correctly, this has absolutely nothing to do with
the findGraphParametersV2 module or the findGraphParameters function
inside that module. It looks to me like you have a *different* module,
called CapitalSimulation, and you try to call it as if it were a
function.

It is difficult to tell exactly what is going on, but my guess is that
inside the CapitalSimulation module you have a function *also* called
CapitalSimulation. So in your module CapitalWithProdV14 (that's the
THIRD module!!!) you probably have some code like:

import CapitalSimulation  # this is a module, not a function

CapitalSimulation(number_of_cycles, capital_advanced, blah blah blah...)


That second line is the problem. You need to change it to:

CapitalSimulation.CapitalSimulation(number_of_cycles, ...)


I think. Like I said, without understanding your code, it's difficult to
be sure exactly what's going on.


Reading between the lines, I feel that perhaps somebody has told you
that you should have one class or one function per file. Or perhaps you
have been reading Java programming books. Either way, it seems to me
that you have an excess of modules and too many confusing imports. That
way leads to frustration.

I believe that you will be much better served to have *one* file per
project, rather than splitting your project into a dozen itty bitty
files. That way you don't need to care about importing your own modules,
because everything is already inside the one file.

If you *must* have separate files, never never never (well, almost
never) give them the same name as the class or function inside them. A
good convention is to name the module in all lower case, and the class
in InitialCaps:

# no, too confusing
CapitalSimulation.CapitalSimulation(...)

# better
capital_simulation.CapitalSimulation(...)


That way, you can tell at a glance which is the module and which is the
class inside the module, and if you make a mistake, it will be more
easily understood:

capital_simulation(...)  # wait a second, that's a module!


[...]
I do not really understand what Steven is recommending below.
Is it an init statement in a file or is it an independent file.
I don't believe this has anything to do with your problem, but for the
record, there is no init statement in Python (although classes do have
an __init__ method). I suggested to Charles that he add an empty
__init__.py file inside his project folder. That would make it an
separate file.


Good luck!




--
Sydney Shall

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to