Ask Your Question
5

Importing Python packages into Sage or Vice Versa

asked 2016-11-06 16:34:37 +0100

Andrew gravatar image

Hi all,

this is my first post. I'm a long time user of Python, and I'm fairly new to Sage. I'm using Ubuntu, if this helps. Currently, python doesn't recognize any of Sage's packages, and Sage can't import even standard Python packages like pandas.

I've looked at various instructions online about how to import sage packages into python and vice versa, but they all seem incredibly intricate, and none of them seem to work for me.

Here are a few attempts that I made:

  1. Running "sudo sagemath --python -m easy_install pandas" in terminal. (Still can't import pandas while running sage; even after running: "sys.path.append('/usr/lib/sagemath/local/lib/python2.7/site-packages/pandas-0.19.1-py2.7-linux-x86_64.egg')" from within sage.)
  2. Following the instructions in the "import sage packages in python" question. (I don't have enough karma to post links.)
  3. Doing various bizzarre attempts at changing sage_root or adding to it my python path.
  4. Attempting to import pip while running sage, and install pandas this way. (Can't import pip.)

I tried a few more things (like downloading packages and "build"ing them, and then doing a few more very technical and annoying things), and all of my attempts fail. Is there really no simple way to do this? What is the most fool-proof way to do this that exists? It would be enormously helpful for me to be able to use python packages while working in sage, or vice versa.

edit retag flag offensive close merge delete

Comments

@Andrew, you should be able to post links now.

slelievre gravatar imageslelievre ( 2016-11-07 08:35:01 +0100 )edit

2 Answers

Sort by » oldest newest most voted
6

answered 2016-11-06 20:01:47 +0100

slelievre gravatar image

Run the sage shell by typing sage -sh in a terminal.

In the sage shell, run pip install pandas.

Next time you launch Sage, you can import pandas.

Before:

$ sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 7.4, Release Date: 2016-10-18                     │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: import pandas
Traceback (most recent call last)
...
ImportError: No module named pandas
sage:

Installing:

$ sage -sh

Starting subshell with Sage environment variables set.  Don't forget
to exit when you are done.  Beware:
 * Do not do anything with other copies of Sage on your system.
 * Do not use this for installing Sage packages using "sage -i" or for
   running "make" at Sage's root directory.  These should be done
   outside the Sage shell.

Bypassing shell configuration files...

Note: SAGE_ROOT=/opt/s/sage-7.4
(sage-sh) you@YourComputer:~$ pip install pandas
Collecting pandas
  Downloading pandas-0.19.1.tar.gz (8.4MB)
    100% |████████████████████████████████| 8.4MB 116kB/s 
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /opt/s/sage-7.4/local/lib/python2.7/site-packages (from pandas)
Requirement already satisfied (use --upgrade to upgrade): pytz>=2011k in /opt/s/sage-7.4/local/lib/python2.7/site-packages (from pandas)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.0 in /opt/s/sage-7.4/local/lib/python2.7/site-packages (from pandas)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /opt/s/sage-7.4/local/lib/python2.7/site-packages (from python-dateutil->pandas)
Installing collected packages: pandas
  Running setup.py install for pandas ... done
Successfully installed pandas-0.19.1
You are using pip version 8.1.2, however version 9.0.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(sage-sh) you@YourComputer:~$

After:

$ sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 7.4, Release Date: 2016-10-18                     │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: import pandas
sage:
edit flag offensive delete link more

Comments

When i run pip install python-igraph it gives that pip install python_igraph pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Collecting python_igraph Could not fetch URL https://pypi.python.org/simple/python-igraph/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping Could not find a version that satisfies the requirement python_igraph (from versions: ) No matching distribution found for python_igraph Could you teach me how to install it correctly?

edenharder gravatar imageedenharder ( 2016-11-07 13:03:35 +0100 )edit

To display blocks of code in questions, answers or comments, select the lines of code, then click the "code" button (the icon with '101 010'). Alternatively, indent each line of code by four spaces. Surrounding code with triple-bacquotes does not work. Can you edit your comment to do that?

slelievre gravatar imageslelievre ( 2016-11-07 13:53:17 +0100 )edit

The error message is complaining that you have no SSL support.

You should install libssl and libssl-dev using your distribution's package manager.

Either type this in a terminal

$ apt-get install libssl libssl-dev

or use the graphical user interface for Ubuntu's package manager.

slelievre gravatar imageslelievre ( 2016-11-07 14:00:08 +0100 )edit

This does not work for the same reason edenharder said. I am using Sage on macOS, and Sage is supposed to be fully self-contained. It should contain all libraries needed. The problem is not that the OS is missing them.

Szabolcs gravatar imageSzabolcs ( 2019-03-01 14:09:23 +0100 )edit

On macOS, you will need to install Apple developer tools by running the following in a terminal:

xcode-select --install

and then to install OpenSSL and recompile Sage's Python as follows (still in a terminal):

sage -i openssl
sage -f python2 python3

Then you can use pip:

  • either do sage -sh then pip install ...
  • or directly sage --pip install ...
slelievre gravatar imageslelievre ( 2019-03-01 17:21:21 +0100 )edit
1

answered 2016-11-06 17:59:36 +0100

mforets gravatar image

updated 2016-11-06 18:19:29 +0100

I usually install new python packages in Sage via ./sage --python -m easy_install <package_name> (no sudo needed). Have you tried this? (if it is successful, any new sage notebook will recognise import <package_name>). Also, which version of sage are you using?

edit flag offensive delete link more

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: 2016-11-06 16:34:37 +0100

Seen: 7,862 times

Last updated: Nov 06 '16