Re: [Tutor] Working with Data files & Lists
On Mon, Jun 2, 2014 at 8:34 AM, JEAN MICHEL wrote: > I'm a Python beginner trying write a program that reads outside txt files, > takes the data like the name and test grades of students then calculate the > average and also assigns a grade and writes the data into a new txt file. > I'm having difficulties writing the program so far I've been able to write > up half of the program but I am noticing there might be bugs. I've tried > running the program but every time I do, my program output is blank and > there is no error messages Wasn't this asked a few weeks ago? This feels hauntingly familiar. Ah, yes, found it. https://mail.python.org/pipermail/tutor/2014-May/101310.html Can you give folks a quick "diff"? Can you explain what has changed since then? That way, folks will know to not repeat something that you already know about. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Swampy: No module name World , (Steven D'Aprano)(Dave Angel)(Alan Gauld)
Thank you very much for the kind replies. apologies for the html being on. Apparently Thunderbird turns it back on when it updates. Should be off now. swampy does contain __init__.py it is completely blank sampy also contains __init__.pyc that does contain a couple of lines ending with: C:\Python27\Lib\site-packages\swampy\__init__.pyt s sys.path in python2 is ['', 'C:\\Python27\\Lib\\idlelib', 'C:\\Windows\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] thanks, I did not know that existed. What is Swampy? It uses Tkinter for gui exercises. http://www.greenteapress.com/thinkpython/swampy/ Module written by Allen Downey to accompany the text 'Think Python' http://www.greenteapress.com/thinkpython and necessary for several sections in the book. Apologies for not explaining this initially. Note, 'Think Python' uses Python2, but this computer has both 2 and 3 on it. They were both working, scripts finding python from any directory, with nothing entered in the win sys Path as long as only one version of python was open at a time. After this problem started I amended the system path %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Python27;C:\python27\scripts adding those last two items. Made a diff for running from console (powershell, actually) but was not needed for using "run module" in Idle. Python3 still works just fine with nothing mentioning it in the path. Location of World module: C:\Python27\Lib\site-packages\swampy\World.py (there is also a compiled file: World.pyc) Swampy modules come up okay as long as the script requesting it is in the Python27 folder. I normally run them from the desktop instead to make it easy to get all of them deleted or stored. So: C:\Python27\my_script.pyworld.mainloop() works also from swampy.World import World works C:\Users\Charles\Desktop\practice_folder\my_script.py swampy.World import World does not work error message today is Traceback (most recent call last): File "", line 1, in from swampy import World EOFError: EOF read where object expected yesterday I was getting a different error, cannot seem to get it to do the same thing today. Yesterday it said there was no module named 'World'. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Working with Data files & Lists
I'm a Python beginner trying write a program that reads outside txt files, takes the data like the name and test grades of students then calculate the average and also assigns a grade and writes the data into a new txt file. I'm having difficulties writing the program so far I've been able to write up half of the program but I am noticing there might be bugs. I've tried running the program but every time I do, my program output is blank and there is no error messages I'm trying to write 4 Functions to extract data from files into Student name, Score1, Score2 and Score 3 lists. then from there I'm trying to create a function that will calculate the average test score using the score1, score2, score3. After that I'm trying to create a function that will to write to file using Student name, lettergrade list. Here is my program so far. def calcaverage(test1,test2,test3): for count in range(test1,test2,test3): curraverage=0 curraverage=((test1[count]+ test2[count]+ test3[count])/3) currentaverage.append(curraverage) if curraverage>= 90: grade= "A" lettergrades.append(grade) elif curraverage >= 80 and curraverage < 90: grade= "B" lettergrades.append(grade) elif curraverage >= 70 and curraverage < 80: grade= "C" lettergrades.append(grade) elif curraverage < 70: grade= "F" lettergrades.append(grade) name=[] test1=[] test2=[] test3=[] averagescore=[] lettergrades=[] with open ("period1.txt", 'r') as infile: for line in infile: values = line.split() name.append(values[0] + ','+ values[1]) for line in infile: values = line.split() score1=float(values[2]) test1.append(score1) for line in infile: values = line.split() score2=float(values[3]) test2.append(score2) for line in inline: values = line.split() score3=float(values[4]) test3.append(score3) averagescore=calcaverage(test1,test2,test3) print(line) ___ 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/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
Re: [Tutor] Swampy: No module name World
On 02/06/2014 02:33, 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. Other modules, like math, import with no problem from other locations import math from math import pi 2*pi 6.28... how can I fix this? thanks in advance As swampy is on pypi you should have installed it using easy_install or pip, so that it is under the site-packages directory. Python would have found it there automatically. I'm guessing that you've downloaded a tar or zip file and manually installed it soemwhere else, am I correct? -- 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
[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
Re: [Tutor] gnupg within a for loop
On 02/06/14 01:35, Danny Yoo wrote: >> Thanks Danny, that was spot on. I actually used os.chdir to change to >> the base directory (which I assigned to a variable) just before the open >> statement. I don't know if that's 'pythonically' correct but it seemed >> like a simple way to do it. Again, thank you for helping me fix this. > > I am glad! I would recommend using os.path.join instead to build absolute > paths, as it's less fragile to external factors. I think of current > working directory the same way I think of global variables: use it > judiciously. Yup, makes sense, thanks for the advice, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] gnupg within a for loop
On 01/06/14 18:28, Danny Yoo wrote: > Hi Adam, > > Ah; I've seen this before. Make sure the file name is either relative > to current working directory, or make the file name absolute. What's > happening is that os.listdir() is giving you file names that are > relative to the base directory you've passed it, but open() doesn't > know about the base directory. > > You can use os.path.join() to create such paths: > > https://docs.python.org/2/library/os.path.html#os.path.join > Thanks Danny, that was spot on. I actually used os.chdir to change to the base directory (which I assigned to a variable) just before the open statement. I don't know if that's 'pythonically' correct but it seemed like a simple way to do it. Again, thank you for helping me fix this. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Swampy: No module name World
On 02/06/14 02:33, 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. It sounds like you installed swampy in the wrong place. Python has a set of locations that it looks for modules. One is the current directory. So if, for example, swampy is in your Python27 folder that would explain it. Other modules, like math, import with no problem from other locations Because the standard modules are in the location where Python looks for them. You can check the sys.path contents to see where Python is looking. If swampy is not in any of those locations Python won't se it. how can I fix this? Probably by reinstalling swampy. But you'll need to check their web site. This list is for core Python and standard library. We can't offer much help for external modules. You are best asking on dedicated forums. 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
Re: [Tutor] Swampy: No module name World
Charles Agriesti Wrote in message: > > from swampy.World import World > world = World() > ImportError: No module name World 1. please use text mail on this mailing-list. Your email software should have an option to change that. 2. You're referring to some nonstandard package called swampy, and the tutor list is dedicated to learning the language and standard library. And you don't even tell us where you got it; did you write it yourself? If not, you'd have better odds either on python-list or on a swampy forum. 3. There are a few standard places that python looks for packages and libraries. Clearly swampy is not installed in any of those places. Please reread the installation instructions for swampy. 4. If there are no such instructions, you can see the default list of places by examining sys.path. import sys print sys.path 5. Generally you should include the whole traceback, not just one line from it. In this case it didn't matter. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor