It's better to import them at the module level (I've been told it's faster when calling the function). However due to circular imports, this is not always possible (unless you lazily import it). Although it sometimes is best to put things in functions that only use a certain module in one (or two) place(s). In short, yes, you are correct, but circular imports force us not to sometimes.
Best, Travis On Saturday, June 22, 2013 2:32:25 AM UTC-7, Matthieu Deneufchâtel wrote: > > Thanks, that works! > I thought it was better to import the modules outside the functions I am > writing. Is there a rule ? > Matthieu > > Le samedi 22 juin 2013 09:56:46 UTC+2, Travis Scrimshaw a écrit : >> >> Hey Matthieu, >> Sounds like a circular import. You'll have to import FreeMonoid in the >> function that you are using it in. For example: >> >> def foo(x): >> from sage.some.module import Bar >> Bar(x) >> ... >> >> Best, >> Travis >> >> >> On Friday, June 21, 2013 3:54:51 PM UTC+2, Matthieu Deneufchâtel wrote: >>> >>> I added the line >>> from sage.monoids.free_monoid import FreeMonoid >>> to the file combinat/words/finite_word.py >>> and after I rebuilt the library, I get an error when I start Sage: >>> /home/matthieu/Documents/sage-5.6/local/lib/python2.7/site-packages/sage/combinat/words/finite_word.py >>> >>> in <module>() >>> 199 from sage.rings.all import Integer, Infinity, ZZ >>> 200 from sage.sets.set import Set >>> --> 201 from sage.monoids.free_monoid import FreeMonoid >>> 202 >>> 203 def tomonoid_element(self): >>> >>> ImportError: cannot import name FreeMonoid >>> Error importing ipy_profile_sage - perhaps you should run %upgrade? >>> WARNING: Loading of ipy_profile_sage failed. >>> Where does it come from? >>> I also tried from sage.monoids.all import * but it did not work either. >>> Matthieu >>> >> -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/groups/opt_out.
