PHP To Python: Development Environment

Python’s venv separates out development environments on a project level basis. It allows you to separate out versions of Python on a per project basis, keeping them independent from the version installed on your local machine used for the various apps you have installed that use Python.

Python version 3 onwards is not backwards compatible with Python 2. You can have both major versions installed on your development machine at the same time without causing problems.

First make sure python3 is installed on your local machine:

python3 -V

If it’s not already installed, install it with:

brew install python3

Create a new self contained Python project. The folder will be created for you if it doesn’t already exist.

python3 -m venv ~/python/my-project/

All you need to do then is jump into your new environment!

source ~/python/my-project/bin/activate

To exit again type:

deactivate

Install pip. pip is to Python what Composer is to PHP, it’s Python’s package manager. It works in a slightly different way to Composer in that you don’t need to manage a configuration file, that information is kept internal to pip.

Check if pip is installed:

pip -V

You can install pip using Python’s easy install tool:

sudo easy_install pip

To install dependencies with pip, use:

pip install modulename

eg:

pip install sklearn

You’ll want to export your projects dependencies so other developers can contribute to your projects. The standard for this is to put a text file title `requirements.txt` in the root of your repository. You can generate this file from pip with the following:

pip freeze > requirements.txt

To install the dependencies from a requirements.txt file, use the following:

pip install -r requirements.txt

Thanks to Oliver Musgrove-Wethey for bringing this method of getting started with Python to my attention.

Leave a Reply

Your email address will not be published. Required fields are marked *