Using Python To Compile Julia Code Natively | by Emmett Boudreau | Towards Data Science

I frequently discuss the benefits of using Julia, as there are many of them. Julia is a scalable, high-performance, and high-level language that is easy to learn and can get nearly any job done. This is especially true for Data Scientists, as Julia’s center of mass is statistical computing and functional program. At the heart of Julia, it was a language created for scientists doing statistics with data, and this direct intent makes it really great at that particular thing.
Although I could talk for years about how Julia is faster, has better floating point accuracy, and is really simple to type, that is not the reason I’m here today. Though I’ve voiced my concerns and likely over-voiced what I love about Julia, there is one big topic I haven’t covered with Julia:
Versatility
One enormous benefit of using the language Python is that Python is universal. If you don’t want to use a web request, there’s probably a wrapper for it, if you want to convert time stamps from Unix to UTC, there’s a library for it. This is a great benefit because it means that your language will always support your tools. I’ve already mentioned that Julia’s packages are its biggest flaw, but with that being said Julia was originally released in 2012, just 8 years ago. Julia counters this by being:
translatable.
Firstly, we have the amazing package PyCall.jl, which allows the calling of Python packages, even within a virtual environment. Being able to call on millions of Python packages is a pretty big deal in the data science space.
The real fruit in the pudding is that the inverse is also possible. That’s right, it is incredibly easy to run Julia natively from Python. This is huge, because as we all know Python can be rather slow at times. For most situations, I can admit, Python is fantastic, but when Python fails you, it can really leave you high and dry without a clue what to do next.
PyJulia, or julia.py allows you to send lines of code like SQL queries, and identify a return variable for the actual return. So in a nutshell, you can run Julia from Python by sending strings to a Julia compiler. The first step is, of course, to grab Julia from your package manager, I’ll be using APT.
sudo apt-get install julia
Now it’s as simple as entering the Julia REPL, then installing PyCall with either ] or using Pkg.
juliajulia> using Pkg; Pkg.add("PyCall")(or)julia> ]pkg> add "PyCall"
For some Julia packages, including PyCall, I recommend building the code:
julia> ]pkg> build "PyCall"
Now, get out of Pkg with backspace and press ctrl/command+D to exit the Julia REPL. We can now install our Python package with pip, I’ll be using pip3.
sudo pip3 install julia
Now that all of our dependencies are met, we can pop into a notebook and try it out! The first step is to use the install method from the Julia library:
import julia as jljl.install()
And now we can import the Julia executable, and run string code with it!
from julia import Juliajl = Julia()average = jl.run("using Lathe.stats: mean; array = [1,5,4,5,6]; m = mean(array); return(m)")
So... Cool!
But what does this mean? Well, any package developed in Julia will be usable in Python. Not only that, the package can also be used in R with JuliaCall.R. I’ve started work on wrappers for the Lathe machine-learning module for Julia that will be usable under native Julia with both Python and R. I’m really excited for the Julia code to both be faster, and make the package universal in the most popular statistical languages simultaneously. This is a big plus for Julia developers because we don’t have to rewrite code in order to maintain a Python and R version. And of course with that comes the actual benefits of using Julia rather than Python at compile time.
Nothing can ever be this simple in computing, so with this awesome method for translating code, there are also some unfortunate issues. Though slim, I’ll admit that having to install Julia and the PyCall dependency is a little tedious, especially for an end-user of a Python package who has never before used Julia. Additionally, Julia versioning and compatibility have caused a huge issue with this package, it can be rough to get PyJulia up and running. The issue that I ran into was primarily in Unix ownership, where compilers were inside of my non-owned directories (owned by root), and I would get exit status 1 because of denied permissions.
This was of course fixed by using chown on my Python location and doing the same for the Julia situation.
Besides that issue, there is also the way that it’s written, which is a little tedious. Sending single lines of code through strings isn’t really optimal in all honesty, and if you’re going to be using quotations in your code, and playing with strings, it’s going to be awful difficult to navigate between “, “””, and ‘ in Python. Not a big deal, but definitely something to be aware of.
To conclude, versatility is a major benefit to Julia. Julia code is not only fast, and extremely scalable, but can also be written into other languages. This is a big plus because your run of the mill Julia package can be run within a wrapper easily, and consistently updated with the original Julia package as it’s updated on the local system, so... Cool! I greatly anticipate getting Lathe figured out for both R and Python so more people can use Lathe and hopefully come to enjoy it.