{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Installating Julia/IJulia on winpython2015-03-27 and after via a Notebook\n",
"###1 - Downloading and Installing the right Julia binary in the right place"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"import io"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# downloading julia (32Mo, may take 1 minute or 2)\n",
"try:\n",
" import urllib.request as urllib2 # Python 3\n",
"except:\n",
" import urllib2 # Python 2\n",
"if 'amd64' in sys.version.lower():\n",
" julia_binary=\"julia-0.3.7-win64.exe\"\n",
" julia_url=\"http://s3.amazonaws.com/julialang/bin/winnt/x64/0.3/julia-0.3.7-win64.exe\"\n",
" hashes=(\"90dedbbee5deb569c613543cc709af85\", \"813f049445421b9531cb6a3a4a2871fdbe1ca5d0\" )\n",
"else:\n",
" julia_binary=\"julia-0.3.7-win32.exe\"\n",
" julia_url=\"http://s3.amazonaws.com/julialang/bin/winnt/x86/0.3/julia-0.3.7-win32.exe\"\n",
" hashes=(\"bd2237758fb2a034174249010cf7ba33\", \"4319c95c38622792b112478d240d2586e08430f0\")\n",
" \n",
"julia_installer=os.environ[\"WINPYDIR\"]+\"\\\\..\\\\tools\\\\\"+julia_binary\n",
"os.environ[\"julia_installer\"]=julia_installer\n",
"g = urllib2.urlopen(julia_url) \n",
"with io.open(julia_installer, 'wb') as f:\n",
" f.write(g.read())\n",
"g.close\n",
"g = None"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Le volume dans le lecteur D s'appelle DATA\n",
" Le num‚ro de s‚rie du volume est 10BD-2ADB\n",
"\n",
" R‚pertoire de D:\\WinPython\\basedir34\\build\\winpython-3.4.3.amd64\\tools\n",
"\n",
"27/03/2015 18:51 36ÿ789ÿ424 julia-0.3.7-win64.exe\n",
" 1 fichier(s) 36ÿ789ÿ424 octets\n",
" 0 R‚p(s) 120ÿ517ÿ468ÿ160 octets libres\n"
]
}
],
"source": [
"#checking it's there\n",
"!dir %julia_installer%"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" MD5 SHA-1 \n",
"-------------------------------- ----------------------------------------\n",
"90dedbbee5deb569c613543cc709af85 813f049445421b9531cb6a3a4a2871fdbe1ca5d0 D:\\WinPython\\basedir34\\build\\winpython-3.4.3.amd64\\python-3.4.3.amd64\\..\\tools\\julia-0.3.7-win64.exe\n"
]
}
],
"source": [
"# checking it's the official julia0.3.2\n",
"import hashlib\n",
"def give_hash(of_file, with_this):\n",
" with io.open(julia_installer, 'rb') as f:\n",
" return with_this(f.read()).hexdigest() \n",
"print (\" \"*12+\"MD5\"+\" \"*(32-12-3)+\" \"+\" \"*15+\"SHA-1\"+\" \"*(40-15-5)+\"\\n\"+\"-\"*32+\" \"+\"-\"*40)\n",
"\n",
"print (\"%s %s %s\" % (give_hash(julia_installer, hashlib.md5) , give_hash(julia_installer, hashlib.sha1),julia_installer))\n",
"assert give_hash(julia_installer, hashlib.md5) == hashes[0]\n",
"assert give_hash(julia_installer, hashlib.sha1) == hashes[1]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"os.environ[\"JULIA_HOME\"] = os.environ[\"WINPYDIR\"]+\"\\\\..\\\\tools\\\\Julia\\\\bin\\\\\"\n",
"os.environ[\"JULIA_EXE\"]=\"julia.exe\"\n",
"os.environ[\"JULIA\"]=os.environ[\"JULIA_HOME\"]+os.environ[\"JULIA_EXE\"]\n",
"# for installation we need this\n",
"os.environ[\"JULIAROOT\"]=os.path.join(os.path.split(os.environ[\"WINPYDIR\"])[0] , 'tools','julia' ) "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# let's install it (add a /S before /D if you want silence mode installation)\n",
"#nullsoft installers don't accept . or .. conventions\n",
"\n",
"# If you are \"USB life style\", or multi-winpython\n",
"# ==> UN-CLICK the OPTION 'CREATE a StartMenuFolder and Shortcut' <== (when it will show up)\n",
"!start cmd /C %julia_installer% /D=%JULIAROOT%\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###2 - Initialize Julia , IJulia, and make them link to winpython"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Writing a julia initial run script, for convenience\n",
"bat_text = r\"\"\"\n",
"@echo off\n",
"set WINPYDIR=%~dp0..\\blablaPYTHON\n",
"set WINPYVER=blablaWINPYVER\n",
"set HOME=%WINPYDIR%\\..\\settings\n",
"set PATH=%WINPYDIR%\\Lib\\site-packages\\PyQt4;%WINPYDIR%\\;%WINPYDIR%\\DLLs;%WINPYDIR%\\Scripts;%WINPYDIR%\\..\\tools;%WINPYDIR%\\..\\tools\\mingw32\\bin;%PATH%;%WINPYDIR%\\..\\tools\\TortoiseHg\n",
"\n",
"set JULIA_HOME=%WINPYDIR%\\..\\tools\\Julia\\bin\\\n",
"if exist \"%JULIA_HOME%\" goto julia_next\n",
"echo --------------------\n",
"echo First install Julia in \\tools\\Julia of winpython\n",
"echo suggestion : don't create Julia shortcuts, nor menu, nor desktop icons \n",
"echo (they would create a .julia in your home directory rather than here)\n",
"echo When it will be done, launch again this .bat\n",
"\n",
"if not exist \"%JULIA_HOME%\" goto julia_end\n",
"\n",
":julia_next\n",
"set SYS_PATH=%PATH%\n",
"set PATH=%JULIA_HOME%;%SYS_PATH%\n",
"\n",
"set JULIA_EXE=julia.exe\n",
"set JULIA=%JULIA_HOME%%JULIA_EXE%\n",
"set private_libdir=bin\n",
"if not exist \"%JULIA_HOME%..\\lib\\julia\\sys.ji\" ( ^\n",
"echo \"Preparing Julia for first launch. This may take a while\" \n",
"echo \"You may see two git related errors. This is completely normal\" \n",
"cd \"%JULIA_HOME%..\\share\\julia\\base\" \n",
"\"%JULIA%\" --build \"%JULIA_HOME%..\\lib\\julia\\sys0\" sysimg.jl \n",
"\"%JULIA%\" --build \"%JULIA_HOME%..\\lib\\julia\\sys\" -J sys0.ji sysimg.jl \n",
"popd && pushd \"%cd%\" )\n",
"\n",
"echo \"julia!\"\n",
"echo --------------------\n",
"echo to install Ijulia for Winpython (the first time) :\n",
"echo type 'julia'\n",
"echo type in Julia prompt 'Pkg.add(\"IJulia\")'\n",
"echo type in Julia prompt 'Pkg.add(\"PyCall\")'\n",
"echo type in Julia prompt 'Pkg.add(\"PyPlot\")'\n",
"echo type in Julia prompt 'Pkg.add(\"Interact\")'\n",
"echo type in Julia prompt 'Pkg.add(\"Compose\")'\n",
"echo type in Julia prompt 'Pkg.add(\"SymPy\")'\n",
"echo type 'Ctrl + 'D' to quit Julia \n",
"echo nota : type 'help()' to get help in Julia\n",
"echo --------------------\n",
"rem 2014-08-23 refinement\n",
"echo or let me do it now\n",
"pause\n",
"echo Pkg.add(\"IJulia\");>this_is_temporary.jl\n",
"echo Pkg.add(\"PyCall\");>>this_is_temporary.jl\n",
"echo Pkg.add(\"PyPlot\");>>this_is_temporary.jl\n",
"echo Pkg.add(\"Interact\");>>this_is_temporary.jl\n",
"echo Pkg.add(\"Compose\");>>this_is_temporary.jl\n",
"echo Pkg.add(\"SymPy\");>>this_is_temporary.jl\n",
"@echo on\n",
"julia this_is_temporary.jl \n",
"@echo off\n",
"\n",
"echo Julia installed\n",
"echo use the \"ijulia_launcher.bat\" script to launch Ijulia directly\n",
"pause\n",
":julia_end\n",
"rem cmd.exe /k\n",
"\"\"\"\n",
"bat_text = bat_text.replace(\"blablaPYTHON\",os.path.split(os.environ[\"WINPYDIR\"])[1])\n",
"bat_text = bat_text.replace(\"blablaWINPYVER\",os.environ[\"WINPYVER\"])\n",
"julia_initializer_bat=os.environ[\"WINPYDIR\"]+\"\\\\..\\\\scripts\\\\initialize_julia_once.bat\"\n",
"if sys.version_info[0] == 3:\n",
" with io.open(julia_initializer_bat, 'w', encoding = sys.getdefaultencoding() ) as f:\n",
" for line in bat_text.splitlines():\n",
" f.write('%s\\n' % line )\n",
"else:\n",
" with io.open(julia_initializer_bat, 'wb' ) as f:\n",
" for line in bat_text.splitlines():\n",
" f.write('%s\\r\\n' % line.encode(sys.getdefaultencoding()) )"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# let's initialize Julia and install \"IJulia\", \"PyCall\", and \"PyPlot\" Julia modules with this .bat just created\n",
"# may take about 10 minutes (Julia pre-compiles itself and download a lot of things)\n",
"!start cmd /C %WINPYDIR%\\\\..\\\\scripts\\\\initialize_julia_once.bat"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# let's launch Ijulia for Ipython now\n",
"# Writing a julia initial run script, for convenience\n",
"bat_text = r\"\"\"\n",
"@echo off\n",
"\n",
"set WINPYDIR=%~dp0..\\blablaPYTHON\n",
"set WINPYVER=blablaWINPYVER\n",
"set HOME=%WINPYDIR%\\..\\settings\n",
"set PATH=%WINPYDIR%\\Lib\\site-packages\\PyQt4;%WINPYDIR%\\;%WINPYDIR%\\DLLs;%WINPYDIR%\\Scripts;%WINPYDIR%\\..\\tools;%WINPYDIR%\\..\\tools\\mingw32\\bin;%PATH%;%WINPYDIR%\\..\\tools\\TortoiseHg\n",
"\n",
"set JULIA_HOME=%WINPYDIR%\\..\\tools\\Julia\\bin\\\n",
"\n",
"set SYS_PATH=%PATH%\n",
"set PATH=%JULIA_HOME%;%SYS_PATH%\n",
"\n",
"set JULIA_EXE=julia.exe\n",
"set JULIA=%JULIA_HOME%%JULIA_EXE%\n",
"\n",
"Ipython notebook --profile julia\n",
"echo to use julia_magic from Ipython, type \"Ipython notebook\" instead.\n",
":julia_end\n",
"cmd.exe /k\n",
"\"\"\"\n",
"bat_text = bat_text.replace(\"blablaPYTHON\",os.path.split(os.environ[\"WINPYDIR\"])[1])\n",
"bat_text = bat_text.replace(\"blablaWINPYVER\",os.environ[\"WINPYVER\"])\n",
"\n",
"ijulia_launcher_bat=os.environ[\"WINPYDIR\"]+\"\\\\..\\\\scripts\\\\ijulia_launcher.bat\"\n",
"if sys.version_info[0] == 3:\n",
" with io.open(ijulia_launcher_bat, 'w', encoding = sys.getdefaultencoding() ) as f:\n",
" for line in bat_text.splitlines():\n",
" f.write('%s\\n' % line )\n",
"else:\n",
" with io.open(ijulia_launcher_bat, 'wb' ) as f:\n",
" for line in bat_text.splitlines():\n",
" f.write('%s\\r\\n' % line.encode(sys.getdefaultencoding()) )\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###3 - Launching IJulia \n",
"\n",
"either from here\n",
"\n",
"either with your new winpython**\\scripts\\ijulia_launcher.bat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!start cmd /C %WINPYDIR%\\\\..\\\\scripts\\\\ijulia_launcher.bat"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now, you should see a IJulia notebook starting next to this IPython Notebook\n",
"\n",
"Check a new Ijulia notebook works with a few julia commands\n",
"````\n",
"println(2+2);\n",
"versioninfo(true)\n",
"````\n",
"\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###4 - Julia Magic ?\n",
"What preceeds should work with all previous version of winpython\n",
"\n",
"This part may only work for recent winpythons "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initializing Julia interpreter. This may take some time...\n"
]
}
],
"source": [
"# first, we must patch the path\n",
"import os\n",
"os.environ[\"JULIA_HOME\"] = os.environ[\"WINPYDIR\"]+\"\\\\..\\\\tools\\\\Julia\\\\bin\\\\\" # \\\\bin\"\n",
"os.environ[\"JULIA_EXE\"]=\"julia.exe\"\n",
"if \"\\\\julia\\\\\" not in os.environ[\"PATH\"].lower():\n",
" os.environ[\"JULIA\"]=os.environ[\"JULIA_HOME\"]+\"\"+os.environ[\"JULIA_EXE\"]\n",
" os.environ[\"PATH\"] =os.environ[\"JULIA_HOME\"]+\";\"+os.environ[\"PATH\"]\n",
"#now we can\n",
"%load_ext julia.magic"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# don't worry if you see \"Julia error ? Failed to initialize PyCall package\" and continue as usual \n",
"%julia @pyimport matplotlib.pyplot as plt\n",
"%julia @pyimport numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%julia\n",
"\n",
"# Note how we mix numpy and julia:\n",
"t = linspace(0, 2*pi, 1000); # use the julia linspace\n",
"s = sin(3 * t + 4 * np.cos(2 * t)); # use the numpy cosine and julia sine\n",
"\n",
"fig = plt.gcf() # **** WATCH THIS VARIABLE ****\n",
"plt.plot(t, s, color=\"red\", linewidth=2.0, linestyle=\"--\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#5 - To Un-install / Re-install Julia and Ijulia (or other trouble-shooting)\n",
"\n",
"If the Ijulia doesn't work because of a \"dead kernel\" the comes every minute\n",
"(or if you just want to un-install Julia 0.3)\n",
"\n",
"\n",
"- stop all Ipython instances that are running \n",
"\n",
"- launch winpython**\\tools\\Julia\\uninstall.exe\n",
"\n",
"- delete the directory winpython**\\settings\\.julia\n",
"\n",
"- remove winpython**\\settings\\.ipython\\default_julia\n",
"\n",
"(maybe check you don't have an annoying c:\\users\\your_windows_profile\\.ipython\\default_julia)\n",
"\n",
"- re-install\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5.656854249492381"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import julia\n",
"j=julia.Julia()\n",
"j.eval(\"1 +31\")\n",
"j.eval(\"sqrt(1 +31)\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}