ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 01 Dec 2014 10:36:17 -0600Executing python modules from packagehttp://ask.sagemath.org/question/25041/executing-python-modules-from-package/I have a project that is primarily written in python but uses sage for some specific operations. In order to facilitate this I have only .py files and use `from sage.all import *` at the top of all modules that make use of sage functionalities. To execute I always used `sage -python path/to/file.py`.
This approach worked fine until it became necessary to split my project into multiple sub-packages. I now want to execute my modules with `python -m package.subpackage.module` for modules that do not use sage (which works) and correspondingly `sage -python -m package.subpackage.module` for modules that do. Unfortunately the latter only returns an error message of the form `~/sage/local/bin/python: No module named package.subpackage`
In order to be able to use package relative imports I am kind of dependent on the -m syntax, so I would like to get it to work. Any ideas what I am doing wrong? Or is this simply not possible with the python that is bundled with sage for internal reasons? Any help is apreciated!Fri, 28 Nov 2014 13:24:51 -0600http://ask.sagemath.org/question/25041/executing-python-modules-from-package/Answer by tmonteil for <p>I have a project that is primarily written in python but uses sage for some specific operations. In order to facilitate this I have only .py files and use <code>from sage.all import *</code> at the top of all modules that make use of sage functionalities. To execute I always used <code>sage -python path/to/file.py</code>. </p>
<p>This approach worked fine until it became necessary to split my project into multiple sub-packages. I now want to execute my modules with <code>python -m package.subpackage.module</code> for modules that do not use sage (which works) and correspondingly <code>sage -python -m package.subpackage.module</code> for modules that do. Unfortunately the latter only returns an error message of the form <code>~/sage/local/bin/python: No module named package.subpackage</code> </p>
<p>In order to be able to use package relative imports I am kind of dependent on the -m syntax, so I would like to get it to work. Any ideas what I am doing wrong? Or is this simply not possible with the python that is bundled with sage for internal reasons? Any help is apreciated!</p>
http://ask.sagemath.org/question/25041/executing-python-modules-from-package/?answer=25049#post-id-25049To what i understand, your package should be located within the Python path of Sage. Note that the command ``python`` launches the python interpreter from your distribution while the command ``sage -python`` launches the python that is shipped with Sage. In order not to interfere with your distribution, the python packages that are shipped with Sage are stored in a separate directory, that is disjoint to the ones where your distribution stores its own python packages. Both pythons use a different path, that is a different set of directories where they have to look in order to find their packages. To see this, you can open a session and type:
>>> import os
>>> os.sys.path
And see the difference in both cases.
So what you need is to add the path of your module in Sage's python path. It is explained [on that question](http://ask.sagemath.org/question/7934/how-do-i-get-sage-to-honor-my-pythonpath-environmental-variable/).
Fri, 28 Nov 2014 20:47:34 -0600http://ask.sagemath.org/question/25041/executing-python-modules-from-package/?answer=25049#post-id-25049Comment by kekx for <p>To what i understand, your package should be located within the Python path of Sage. Note that the command <code>python</code> launches the python interpreter from your distribution while the command <code>sage -python</code> launches the python that is shipped with Sage. In order not to interfere with your distribution, the python packages that are shipped with Sage are stored in a separate directory, that is disjoint to the ones where your distribution stores its own python packages. Both pythons use a different path, that is a different set of directories where they have to look in order to find their packages. To see this, you can open a session and type:</p>
<pre><code>>>> import os
>>> os.sys.path
</code></pre>
<p>And see the difference in both cases.</p>
<p>So what you need is to add the path of your module in Sage's python path. It is explained <a href="http://ask.sagemath.org/question/7934/how-do-i-get-sage-to-honor-my-pythonpath-environmental-variable/">on that question</a>.</p>
http://ask.sagemath.org/question/25041/executing-python-modules-from-package/?comment=25069#post-id-25069Thanks! This was very helpful for figuring out the problem.Mon, 01 Dec 2014 10:34:13 -0600http://ask.sagemath.org/question/25041/executing-python-modules-from-package/?comment=25069#post-id-25069Answer by kekx for <p>I have a project that is primarily written in python but uses sage for some specific operations. In order to facilitate this I have only .py files and use <code>from sage.all import *</code> at the top of all modules that make use of sage functionalities. To execute I always used <code>sage -python path/to/file.py</code>. </p>
<p>This approach worked fine until it became necessary to split my project into multiple sub-packages. I now want to execute my modules with <code>python -m package.subpackage.module</code> for modules that do not use sage (which works) and correspondingly <code>sage -python -m package.subpackage.module</code> for modules that do. Unfortunately the latter only returns an error message of the form <code>~/sage/local/bin/python: No module named package.subpackage</code> </p>
<p>In order to be able to use package relative imports I am kind of dependent on the -m syntax, so I would like to get it to work. Any ideas what I am doing wrong? Or is this simply not possible with the python that is bundled with sage for internal reasons? Any help is apreciated!</p>
http://ask.sagemath.org/question/25041/executing-python-modules-from-package/?answer=25070#post-id-25070Alright, it turned out that the problem was/is actually related to [this other problem](http://ask.sagemath.org/question/25043/untrusted-group-warning/) that I am also having. In short, for security reasons sage is stripping `os.sys.path` of the first entry which is usually the empty string and tells python to look in the current directory.Mon, 01 Dec 2014 10:36:17 -0600http://ask.sagemath.org/question/25041/executing-python-modules-from-package/?answer=25070#post-id-25070