r/Python • • Aug 05 '19

Python's response to MATLAB

Post image
4.3k Upvotes

372 comments sorted by

View all comments

60

u/Antouziast Aug 05 '19

Young engineer here, PhD student. I have to say Matlab is an awesome tool when it comes to manipulating and exploring data really fast but now I'd love to expand my programming skills and turn towards Python. But on my day to day work it's really hard to branch out since a lot of my tools are written in Matlab.

Also, one of the biggest issues I've encountered with trying to learn Python is how to set up your environment and workflow before even writing a single line of code. I've looked into conda and all but it takes some energy to find the good ressources that will help when you've never used anything else than Matlab which just works out of the box. (Any awesome tutorial to give good practices as to the management of a project in and around the code? i.e. "I'm going to start a project xyz, what's a good practice to manage my code and dependencies in a not cluttery way so that I can start working on the code and not fighting bugs?")

But in the long run I hope to rely more and more on Python than on Matlab!

TL;DR : I'm a Matlab user that wants to avoid the mistakes you do at the beginning when learning Python

9

u/tunisia3507 Aug 05 '19

I use pyenv for python environments. It's great. It isolates the python binaries you're fucking around with from whatever your system comes with and relies on.

# install pyenv
curl https://pyenv.run | bash

# restart your shell however you like

# install whichever versions of python you like
BEST_PYTHON=3.7.4
pyenv install $BEST_PYTHON

Then whenever you start a new project

# create the virtual environment
pyenv virtualenv $BEST_PYTHON myproject

# automatically activate that environment whenever you're in this directory
pyenv local myproject

# install some dependencies
pip install ipython numpy matplotlib

# save them so you can rebuild the environment easily
pip freeze > requirements.txt

Development environments should not be a teetering pile of environment variables, dependencies and cross-links. They should be ephemeral, easily trashed and recreated, wherever possible. They should serve one project and one project only. MATLAB only works out of the box as far as python works out of the box - with its built-in modules. Once you start downloading extra snippets of code you have to dump everything into one directory manually, or change your matlab path, or whatever, where in python it's just pip install thatpackage, write down the package in your requirements.txt, and it'll be trivial to fetch again next time you need it.

Packaging stuff for distribution is a little more tricky, but MATLAB just doesn't bother with that at all, so you can't knock python for it.

3

u/Antouziast Aug 05 '19

I actually think that this philosophy makes more sense. Once a project gets complicated in Matlab you end up with a lot of junk files in the working directory.