{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# Ways to install a module\n", "\n", "Install a module from a notebook."]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [{"data": {"text/html": ["
run previous cell, wait for 2 seconds
\n", ""], "text/plain": [""]}, "execution_count": 2, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import add_notebook_menu\n", "add_notebook_menu()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The module [pymyinstall](http://www.xavierdupre.fr/app/pymyinstall/helpsphinx/) proposes an easy to install module mostly on Windows as it is already quite easy to do it on Linux/Mac through [pip](http://pip.readthedocs.org/en/latest/). There are a couple of ways to install a module and they should be tried in that way:\n", "\n", "* **pip**: pip\n", "* **wheel**: use also pip but on Windows, it will fetch the wheel file from the location mentioned below (Unofficial...)\n", "* **github**: source (usually from [github](https://github.com/)), the owner of the source must be specified\n", "\n", "Old way which should not be used anymore:\n", "\n", "* exe: a setup on Windows (only on Windows), replaced by pip in Linux/Max\n", "* exe_xd: only for Windows, some setups gathered on my website\n", " \n", "[pip](http://pip.readthedocs.org/en/latest/) is great because it deals with dependencies for you. I recommend to use Pyhon 3.4 because that is the first version which includes ``pip``. It takes the modules from [PyPI](https://pypi.python.org/pypi). The only drawback happens on Windows when a module includes [Fortran](http://en.wikipedia.org/wiki/Fortran)/[C](http://en.wikipedia.org/wiki/C_%28programming_language%29)/[C++](http://en.wikipedia.org/wiki/C%2B%2B) files which must be compiled. As opposed to Linux or Mac with [gcc](https://gcc.gnu.org/), there is not an official compiler to handle every package and it has to be installed first. On Windows, it can be done by installing [Visual Studio Express 2010](http://www.visualstudio.com/downloads/download-visual-studio-vs#DownloadFamilies_4) but sometimes, the dependencies can be tricky. That's why it is recommended to install already compiled python extensions. Not every module provides a compiled version but there exists two main ways to get them:\n", "\n", "* [Unofficial Windows Binaries for Python Extension Packages](http://www.lfd.uci.edu/~gohlke/pythonlibs/)\n", "* [Anaconda](http://continuum.io/)\n", "\n", "[pymysintall](http://www.xavierdupre.fr/app/pymyinstall/helpsphinx/) takes the setup from the first source. The following snippets of code gives an example for each described way to install a module. The setup way only works on Windows."]}, {"cell_type": "markdown", "metadata": {}, "source": ["### pip\n", "\n", "Let's try with the following module: [numbers_extractor](https://pypi.python.org/pypi/numbers_extractor)."]}, {"cell_type": "code", "execution_count": 2, "metadata": {"collapsed": true}, "outputs": [], "source": ["from pymyinstall import ModuleInstall"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The following only works if you have enough permissions to do it which is the case if the notebook server is started with admin permissions or if you are using a virtual environment."]}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["installation of numbers_extractor:pip:import numbers_extractor\n"]}, {"data": {"text/plain": ["True"]}, "execution_count": 4, "metadata": {}, "output_type": "execute_result"}], "source": ["ModuleInstall(\"numbers_extractor\", \"pip\").install()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### wheel (for files *.whl)"]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [{"data": {"text/plain": ["True"]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["ModuleInstall(\"numpy\", \"wheel\").install()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The function checks first if the module was already installed. That's why it displays only True."]}, {"cell_type": "markdown", "metadata": {}, "source": ["All modules are not available at [Unofficial Windows Binaries for Python Extension Packages](http://www.lfd.uci.edu/~gohlke/pythonlibs/) such as [xgboost](https://github.com/dmlc/xgboost). For this one, ``wheel`` must be replaced by ``wheel2``. The wheel file will be picked from [xavierdupre.fr](http://www.xavierdupre.fr)."]}, {"cell_type": "markdown", "metadata": {}, "source": ["### github\n", "\n", "[github](https://github.com/) holds the source of most of the open source project. This one is no exception. You can check this page for example [Top 400 Python Projects in Github](http://pythonhackers.com/open-source/). We try here with the module [bottle](https://github.com/defnull/bottle/)."]}, {"cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["installation of bottle:github:import bottle\n", "downloading https://github.com/defnull/bottle/archive/master.zip\n", "unzipping .\\bottle.zip\n", " creating folder .\\bottle-master\n", " unzipped bottle-master/.coveragerc to .\\bottle-master/.coveragerc\n", " unzipped bottle-master/.gitignore to .\\bottle-master/.gitignore\n", " unzipped bottle-master/.travis.yml to .\\bottle-master/.travis.yml\n", " unzipped bottle-master/AUTHORS to .\\bottle-master/AUTHORS\n", " unzipped bottle-master/LICENSE to .\\bottle-master/LICENSE\n", " unzipped bottle-master/MANIFEST.in to .\\bottle-master/MANIFEST.in\n", " unzipped bottle-master/Makefile to .\\bottle-master/Makefile\n", " unzipped bottle-master/README.rst to .\\bottle-master/README.rst\n", " unzipped bottle-master/bottle.py to .\\bottle-master/bottle.py\n", " creating folder .\\bottle-master/docs\n", " creating folder .\\bottle-master/docs/_locale\n", " unzipped bottle-master/docs/_locale/README.txt to .\\bottle-master/docs/_locale/README.txt\n", " unzipped bottle-master/docs/_locale/update.sh to .\\bottle-master/docs/_locale/update.sh\n", " creating folder .\\bottle-master/docs/_locale/zh_CN\n", " unzipped bottle-master/docs/_locale/zh_CN/api.po to .\\bottle-master/docs/_locale/zh_CN/api.po\n", " unzipped bottle-master/docs/_locale/zh_CN/async.po to .\\bottle-master/docs/_locale/zh_CN/async.po\n", " unzipped bottle-master/docs/_locale/zh_CN/changelog.po to .\\bottle-master/docs/_locale/zh_CN/changelog.po\n", " unzipped bottle-master/docs/_locale/zh_CN/configuration.po to .\\bottle-master/docs/_locale/zh_CN/configuration.po\n", " unzipped bottle-master/docs/_locale/zh_CN/contact.po to .\\bottle-master/docs/_locale/zh_CN/contact.po\n", " unzipped bottle-master/docs/_locale/zh_CN/deployment.po to .\\bottle-master/docs/_locale/zh_CN/deployment.po\n", " unzipped bottle-master/docs/_locale/zh_CN/development.po to .\\bottle-master/docs/_locale/zh_CN/development.po\n", " unzipped bottle-master/docs/_locale/zh_CN/faq.po to .\\bottle-master/docs/_locale/zh_CN/faq.po\n", " unzipped bottle-master/docs/_locale/zh_CN/index.po to .\\bottle-master/docs/_locale/zh_CN/index.po\n", " unzipped bottle-master/docs/_locale/zh_CN/plugindev.po to .\\bottle-master/docs/_locale/zh_CN/plugindev.po\n", " unzipped bottle-master/docs/_locale/zh_CN/plugins.po to .\\bottle-master/docs/_locale/zh_CN/plugins.po\n", " unzipped bottle-master/docs/_locale/zh_CN/recipes.po to .\\bottle-master/docs/_locale/zh_CN/recipes.po\n", " unzipped bottle-master/docs/_locale/zh_CN/routing.po to .\\bottle-master/docs/_locale/zh_CN/routing.po\n", " unzipped bottle-master/docs/_locale/zh_CN/stpl.po to .\\bottle-master/docs/_locale/zh_CN/stpl.po\n", " unzipped bottle-master/docs/_locale/zh_CN/tutorial.po to .\\bottle-master/docs/_locale/zh_CN/tutorial.po\n", " unzipped bottle-master/docs/_locale/zh_CN/tutorial_app.po to .\\bottle-master/docs/_locale/zh_CN/tutorial_app.po\n", " unzipped bottle-master/docs/api.rst to .\\bottle-master/docs/api.rst\n", " unzipped bottle-master/docs/async.rst to .\\bottle-master/docs/async.rst\n", " unzipped bottle-master/docs/changelog.rst to .\\bottle-master/docs/changelog.rst\n", " unzipped bottle-master/docs/conf.py to .\\bottle-master/docs/conf.py\n", " unzipped bottle-master/docs/configuration.rst to .\\bottle-master/docs/configuration.rst\n", " unzipped bottle-master/docs/contact.rst to .\\bottle-master/docs/contact.rst\n", " unzipped bottle-master/docs/deployment.rst to .\\bottle-master/docs/deployment.rst\n", " unzipped bottle-master/docs/development.rst to .\\bottle-master/docs/development.rst\n", " unzipped bottle-master/docs/faq.rst to .\\bottle-master/docs/faq.rst\n", " unzipped bottle-master/docs/index.rst to .\\bottle-master/docs/index.rst\n", " unzipped bottle-master/docs/plugindev.rst to .\\bottle-master/docs/plugindev.rst\n", " creating folder .\\bottle-master/docs/plugins\n", " unzipped bottle-master/docs/plugins/index.rst to .\\bottle-master/docs/plugins/index.rst\n", " unzipped bottle-master/docs/recipes.rst to .\\bottle-master/docs/recipes.rst\n", " unzipped bottle-master/docs/routing.rst to .\\bottle-master/docs/routing.rst\n", " unzipped bottle-master/docs/stpl.rst to .\\bottle-master/docs/stpl.rst\n", " unzipped bottle-master/docs/tutorial.rst to .\\bottle-master/docs/tutorial.rst\n", " unzipped bottle-master/docs/tutorial_app.rst to .\\bottle-master/docs/tutorial_app.rst\n", " unzipped bottle-master/setup.cfg to .\\bottle-master/setup.cfg\n", " unzipped bottle-master/setup.py to .\\bottle-master/setup.py\n", " creating folder .\\bottle-master/test\n", " unzipped bottle-master/test/.coveragerc to .\\bottle-master/test/.coveragerc\n", " unzipped bottle-master/test/build_python.sh to .\\bottle-master/test/build_python.sh\n", " unzipped bottle-master/test/servertest.py to .\\bottle-master/test/servertest.py\n", " unzipped bottle-master/test/test_auth.py to .\\bottle-master/test/test_auth.py\n", " unzipped bottle-master/test/test_config.py to .\\bottle-master/test/test_config.py\n", " unzipped bottle-master/test/test_configdict.py to .\\bottle-master/test/test_configdict.py\n", " unzipped bottle-master/test/test_contextlocals.py to .\\bottle-master/test/test_contextlocals.py\n", " unzipped bottle-master/test/test_environ.py to .\\bottle-master/test/test_environ.py\n", " unzipped bottle-master/test/test_fileupload.py to .\\bottle-master/test/test_fileupload.py\n", " unzipped bottle-master/test/test_formsdict.py to .\\bottle-master/test/test_formsdict.py\n", " unzipped bottle-master/test/test_importhook.py to .\\bottle-master/test/test_importhook.py\n", " unzipped bottle-master/test/test_jinja2.py to .\\bottle-master/test/test_jinja2.py\n", " unzipped bottle-master/test/test_mako.py to .\\bottle-master/test/test_mako.py\n", " unzipped bottle-master/test/test_mdict.py to .\\bottle-master/test/test_mdict.py\n", " unzipped bottle-master/test/test_mount.py to .\\bottle-master/test/test_mount.py\n", " unzipped bottle-master/test/test_outputfilter.py to .\\bottle-master/test/test_outputfilter.py\n", " unzipped bottle-master/test/test_plugins.py to .\\bottle-master/test/test_plugins.py\n", " unzipped bottle-master/test/test_resources.py to .\\bottle-master/test/test_resources.py\n", " unzipped bottle-master/test/test_route.py to .\\bottle-master/test/test_route.py\n", " unzipped bottle-master/test/test_router.py to .\\bottle-master/test/test_router.py\n", " unzipped bottle-master/test/test_securecookies.py to .\\bottle-master/test/test_securecookies.py\n", " unzipped bottle-master/test/test_sendfile.py to .\\bottle-master/test/test_sendfile.py\n", " unzipped bottle-master/test/test_server.py to .\\bottle-master/test/test_server.py\n", " unzipped bottle-master/test/test_stpl.py to .\\bottle-master/test/test_stpl.py\n", " unzipped bottle-master/test/test_wsgi.py to .\\bottle-master/test/test_wsgi.py\n", " unzipped bottle-master/test/testall.py to .\\bottle-master/test/testall.py\n", " unzipped bottle-master/test/tools.py to .\\bottle-master/test/tools.py\n", " unzipped bottle-master/test/travis-requirements.txt to .\\bottle-master/test/travis-requirements.txt\n", " unzipped bottle-master/test/travis_setup.sh to .\\bottle-master/test/travis_setup.sh\n", " creating folder .\\bottle-master/test/views\n", " unzipped bottle-master/test/views/jinja2_base.tpl to .\\bottle-master/test/views/jinja2_base.tpl\n", " unzipped bottle-master/test/views/jinja2_inherit.tpl to .\\bottle-master/test/views/jinja2_inherit.tpl\n", " unzipped bottle-master/test/views/jinja2_simple.tpl to .\\bottle-master/test/views/jinja2_simple.tpl\n", " unzipped bottle-master/test/views/mako_base.tpl to .\\bottle-master/test/views/mako_base.tpl\n", " unzipped bottle-master/test/views/mako_inherit.tpl to .\\bottle-master/test/views/mako_inherit.tpl\n", " unzipped bottle-master/test/views/mako_simple.tpl to .\\bottle-master/test/views/mako_simple.tpl\n", " unzipped bottle-master/test/views/stpl_include.tpl to .\\bottle-master/test/views/stpl_include.tpl\n", " unzipped bottle-master/test/views/stpl_no_vars.tpl to .\\bottle-master/test/views/stpl_no_vars.tpl\n", " unzipped bottle-master/test/views/stpl_simple.tpl to .\\bottle-master/test/views/stpl_simple.tpl\n", " unzipped bottle-master/test/views/stpl_t2base.tpl to .\\bottle-master/test/views/stpl_t2base.tpl\n", " unzipped bottle-master/test/views/stpl_t2inc.tpl to .\\bottle-master/test/views/stpl_t2inc.tpl\n", " unzipped bottle-master/test/views/stpl_t2main.tpl to .\\bottle-master/test/views/stpl_t2main.tpl\n", " unzipped bottle-master/test/views/stpl_unicode.tpl to .\\bottle-master/test/views/stpl_unicode.tpl\n", " unzipped bottle-master/tox.ini to .\\bottle-master/tox.ini\n", "install C\n", "warning: Successfully installed not found\n"]}, {"data": {"text/plain": ["True"]}, "execution_count": 6, "metadata": {}, "output_type": "execute_result"}], "source": ["ModuleInstall(\"bottle\", \"github\", \"defnull\").install()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The function downloads the source from GitHub and install them using the instruction ``python setup.py install``. The function still has to be improved because analyzing the output is always obvious if there are dependencies or error messages. We check again a second call does not install the module again."]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["installation of bottle:github:import bottle\n", "unzipping .\\bottle.zip\n", "install C\n", "warning: Successfully installed not found\n"]}, {"data": {"text/plain": ["True"]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["ModuleInstall(\"bottle\", \"github\", \"defnull\").install()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["We finally check the module can be imported. It sometimes requires the kernel to restarted."]}, {"cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": ["import bottle"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The two previous methods download files and create others when some file needs to be uncompressed."]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [{"data": {"text/plain": ["['bottle-master',\n", " 'bottle.zip',\n", " 'install_module.ipynb',\n", " 'python-snappy-0.5.win32-py3.4.exe']"]}, "execution_count": 9, "metadata": {}, "output_type": "execute_result"}], "source": ["import os\n", "os.listdir(\".\")"]}, {"cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": []}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0"}}, "nbformat": 4, "nbformat_minor": 2}