Mailing List Archive

Silly (maybe) question re imported module(s)
Morning All

I'm working through the tutorial and running / saving work that I wish to keep
and build on, most times I can save and (re)import later with no difference to
when typed in console or editor and run with F5 (which saves before it can run)


But sometimes saved work (albeit small) when imported does not work any longer

I assume I have missed something, any pointers to what/how/why please




--
https://mail.python.org/mailman/listinfo/python-list
Re: Silly (maybe) question re imported module(s) [ In reply to ]
On 19/05/2023 07:44, Grizzy Adams via Python-list wrote:

> when typed in console or editor and run with F5 (which saves before it can run)
>
> But sometimes saved work (albeit small) when imported does not work any longer

Looks like you are using IDLE? If so, it's possible that in the original
case you had some values set from a previous run that were picked up but
when you reimport later those values are missing. It would help if you
can post any error messages since they should give clues(like name
errors for example).

Also describe how you are "importing" the modules. are you typing

>>> import mymodule

at the interactive prompt or are you using the File->Open menu
to load them into the editor? (It sounds like the latter)

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


--
https://mail.python.org/mailman/listinfo/python-list
Re: Silly (maybe) question re imported module(s) [ In reply to ]
> On 19 May 2023, at 07:44, Grizzy Adams via Python-list <python-list@python.org> wrote:
>
> Morning All
>
> I'm working through the tutorial and running / saving work that I wish to keep
> and build on, most times I can save and (re)import later with no difference to
> when typed in console or editor and run with F5 (which saves before it can run)
>
>
> But sometimes saved work (albeit small) when imported does not work any longer
>
> I assume I have missed something, any pointers to what/how/why please

reimport is not 100% reliable, in simple cases it will work, but as your
code gets more complex it can break.

I rarely use reimport. Usually just exit python and start again.

If you are working on scripts then just run them from the Terminal
will work well with any need to use reimport.

Barry

>
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

--
https://mail.python.org/mailman/listinfo/python-list
Re: Silly (maybe) question re imported module(s) [ In reply to ]
Friday, May 19, 2023 at 12:25, Barry Scott wrote:
Re: Silly (maybe) question re impor (at least in part)

>
>
>> On 19 May 2023, at 07:44, Grizzy Adams via Python-list <python-list@python.org> wrote:
>>
>> Morning All
>>
>> I'm working through the tutorial and running / saving work that I wish to keep
>> and build on, most times I can save and (re)import later with no difference to
>> when typed in console or editor and run with F5 (which saves before it can run)
>>
>> But sometimes saved work (albeit small) when imported does not work any longer

>> I assume I have missed something, any pointers to what/how/why please

>reimport is not 100% reliable, in simple cases it will work, but as your
>code gets more complex it can break.

at the moment my code is very minimal, one saved piece is a nice one-liner that
I kept because I wanted to remember syntax and how easy/elegant "some" things
are in python, but if I import it it fails

>I rarely use reimport. Usually just exit python and start again.

I may have confused this I use import to get my saved work, the "re" was to
shoe it had been saved by the editor

>If you are working on scripts then just run them from the Terminal
>will work well with any need to use reimport.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Silly (maybe) question re imported module(s) [ In reply to ]
On 19/05/2023 13:36, Grizzy Adams wrote:

>> Looks like you are using IDLE?
>
> Yes
>
>
> From consol
>
>>>> import cube
>>>> cubes
> Traceback (most recent call last):
> File "<pyshell#1>", line 1, in <module>
> cubes
> NameError: name 'cubes' is not defined

You imported cube but tried to use cubes. I'm guessing
cubes is defined inside cube so you would need

cube.cubes

> File->Open mymodule.module into the editor, then F5 to run all is well
>
>>>>
> ================ RESTART: D:\Shades\Tools\Python\Temp\cube.py ================
>>>> cubes
> [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]


cubes looks like it should be a function but in that case there
should be parens after it. So I'm not sure how that is working!

I'd expect that you need to do:

import cube
cube.cubes()

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


--
https://mail.python.org/mailman/listinfo/python-list
Re: Silly (maybe) question re imported module(s) [ In reply to ]
On 19/05/2023 19:46, Grizzy Adams wrote:

> Tried that
> ================ RESTART: D:\Shades\Tools\Python\Temp\cube.py ================
>>>> cubes()
> Traceback (most recent call last):
> File "<pyshell#0>", line 1, in <module>
> cubes()
> TypeError: 'list' object is not callable

Ah, now I understand. cubes is a literal list defined in the module.


> But that was spot on, thanks
>
>>>> import cube
>>>> cube.cubes
> [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

Glad to help.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


--
https://mail.python.org/mailman/listinfo/python-list