Hi On 2018-11-19, Kolen Cheung <christian.ko...@gmail.com> wrote: > Then I thought I can import it in Python like this: > > import sage.rings > # OK > > sage.rings.polynomial.polynomial_ring.PolynomialRing_field > # AttributeError
Admittedly the following is not an ideal solution, but you can do >>> from sage import all >>> from sage.rings.polynomial.polynomial_ring import PolynomialRing_field > sage.rings.polynomial.polynomial_ring.PolynomialRing_field_with_category The "..._with_category" classes cannot be imported: These are dynamically created classes. > How would you write the same program in Python using sage as a library? And > in general I see many unfamiliar syntax (from Python’s point of view) like > R.<x>, QQ[], (0..20), etc. That's a feature. Syntax such as R.<x> = QQ[] was inspired from Magma (if I recall correctly). This and other syntactical features are very useful in interactive sessions. It works because of a preparser. If you want to know how to translate an interactive session into module code, the "preparse" function is your friend: sage: preparse("R.<x> = QQ[]") "R = QQ['x']; (x,) = R._first_ngens(1)" So, the output of preparse(`interactive sage command`) is `valid python command`. Concerning imports of constants, there is the "import_statements" function: sage: R.<x> = QQ[] sage: import_statements(R) from sage.rings.qqbar import QQx sage: R.<x,y> = QQ[] sage: import_statements(R) from sage.rings.qqbar import QQxy sage: R.<x,y,z> = QQ[] sage: import_statements(R) Traceback (most recent call last): ... ValueError: no import statement found for 'Multivariate Polynomial Ring in x, y, z over Rational Field'. So, "import_statements" is for stuff that is defined in some Sage module, not for interactively created objects If I recall correctly, there is a function that for *many* (not all) interactively created objects in Sage tells how they can be constructed, but I don't recall the name of that function. > Do you think it is realistic to use sage as a > Python library and completely not using sage (as an interpreter) itself? Sure. If you write code (in the sense of "python or cython module") for Sage, then you are in fact using Sage as a Python library. To execute it in Python, I think "from sage import all" should be enough to make it work (but a less thorough import statement might work too). Best regards, Simon -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at https://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.