Re: [Tutor] How does one construct a module for import?
On 02/06/2014 17:08, Sydney Shall wrote: Would you please be kind enough to stop top posting, it makes following a thread difficult, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does one construct a module for import?
Thanks Steven I will try what you recommend. You were correct. Not understanding properly, I had constucted both a directory and a file in my module directory. I can now correct it, I think. Thanks for all your help Sydney On 02/06/2014 17:47, Steven D'Aprano wrote: 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 staff29 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. -- Sydney Shall ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does one construct a module for import?
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 staff29 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does one construct a module for import?
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 staff29 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. in () > 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 CapitalWithProd
Re: [Tutor] How does one construct a module for import?
On Mon, Jun 02, 2014 at 03:41:15PM +0100, Sydney Shall wrote: > Alan, > Please forgive me, but I am still unclear. > Do you mean that I must add a file called __ini__.py to my folder or do > you mean that each file that I wish to import should have that statement > [ __init__.py ] immediately after (I presume) my def statement? Neither. Forget about __init__.py, that doesn't seem to be related to your problem. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does one construct a module for import?
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. > in () > > 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 recomme
Re: [Tutor] How does one construct a module for import?
Alan, Please forgive me, but I am still unclear. Do you mean that I must add a file called __ini__.py to my folder or do you mean that each file that I wish to import should have that statement [ __init__.py ] immediately after (I presume) my def statement? If it must be a file, what is in this file? With many thanks, Sydney On 02/06/2014 14:52, Alan Gauld wrote: On 02/06/14 13:21, Sydney Shall wrote: I do not really understand what Steven is recommending below. Is it an init statement in a file or is it an independent file. It is an independent file (which can be empty) whose existence indicates to python that a folder is a package. Thus if you have a folder abc which contains files a,b and c. Even if abc is in Python's import path you cannot import a,b or c unless there is an __init__.py HTH -- Sydney Shall ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does one construct a module for import?
On 6/2/2014 5:21 AM, Sydney Shall wrote: I am having a similar problem. However, when I use the import statement in my program I get a runtime error as follows: in () > 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) 525 lnsurplusvalue, lnvariablecapital, lnconstantcapital, 526 lnlabourpowers, lnnewvaluecreated, rationvtoac, --> 527 rationvtorc) this is the end of something (as per the closing paren) The error message would indicate that the name on the start of this something is a module name. so you've likely done something like: import xyz xyz(a,b,c,d,e...,rationvtorc) when you should be doing something like: import xyz xyz.callable(a,b,c,d,e...,rationvtorc) HTH, Emile 528 529 plotDataV2(cycles, AdvancedCapital, lnAdvancedCapital, RealisedCapital, TypeError: 'module' object is not callable I do not really understand what Steven is recommending below. Is it an init statement in a file or is it an independent file. Thanks to you all. Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does one construct a module for import?
On 02/06/14 13:21, Sydney Shall wrote: I do not really understand what Steven is recommending below. Is it an init statement in a file or is it an independent file. It is an independent file (which can be empty) whose existence indicates to python that a folder is a package. Thus if you have a folder abc which contains files a,b and c. Even if abc is in Python's import path you cannot import a,b or c unless there is an __init__.py HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How does one construct a module for import?
I am having a similar problem. 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 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'] However, when I use the import statement in my program I get a runtime error as follows: in () > 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) 525 lnsurplusvalue, lnvariablecapital, lnconstantcapital, 526 lnlabourpowers, lnnewvaluecreated, rationvtoac, --> 527 rationvtorc) 528 529 plotDataV2(cycles, AdvancedCapital, lnAdvancedCapital, RealisedCapital, TypeError: 'module' object is not callable I do not really understand what Steven is recommending below. Is it an init statement in a file or is it an independent file. Thanks to you all. Sydney On 02/06/2014 03:55, Steven D'Aprano wrote: On Sun, Jun 01, 2014 at 08:33:37PM -0500, Charles Agriesti wrote: from swampy.World import World world = World() ImportError: No module name World These scripts run with no problem as long as the file location is the python27 folder. But not from outside the folder. Where does swampy come from? Is it your module? Somebody else's? It looks to me like it is a bug in swampy. You are trying to use it as if it were a package containing a sub-module, but it isn't actually a package, just a folder full of modules. If swampy is somebody else's project, you should report this to them as a bug, but if it is yours, then you should be able to fix it by adding an empty __init__.py file inside the swampy folder. My guess is that you have a folder like this inside the python27 folder: python27 +-- [other python files] +-- swampy +-- World.py but you need this: python27 +-- [other python files] +-- swampy +-- __init__.py +-- World.py If this is not what you have, you will need to explain in more detail what the layout of your files is, where swampy came from, and where it is. -- Sydney Shall ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor