Ask Your Question
0

Executing python modules from package

asked 2014-11-28 20:24:51 +0100

kekx gravatar image

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!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-12-01 17:36:17 +0100

kekx gravatar image

Alright, it turned out that the problem was/is actually related to this other problem 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.

edit flag offensive delete link more
0

answered 2014-11-29 03:47:34 +0100

tmonteil gravatar image

updated 2014-11-29 03:50:10 +0100

To 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.

edit flag offensive delete link more

Comments

Thanks! This was very helpful for figuring out the problem.

kekx gravatar imagekekx ( 2014-12-01 17:34:13 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-11-28 20:24:51 +0100

Seen: 549 times

Last updated: Dec 01 '14