4
VIRTUALENV FOR MAC & LINUX: LOGICALLY ABSTRACTING YOUR PYTHON PROJECTS FOR MAC & LINUX

VIRTUALENV FOR MAC & LINUX

LOGICALLY ABSTRACTING YOUR PYTHON PROJECTS FOR MAC & LINUX

PYTHON 3, VIRTUALENV

EASY

last hacked on Sep 24, 2017

Access to a unix terminal is a requirement for this project. `python3` is a pre-requisite to this project. Make sure you have it installed in your system. Check your `/usr/bin/` directory for `python3`; if you can't find it and/or need to install it, check [python.org/downloads](https://www.python.org/downloads/). # Install VirtualEnv On your terminal, use `pip3` to install `virtualenv`: $ pip3 install virtualenv Verify your installation using `pip3 list` on your terminal and check virtualenv is listed using grep: $ pip3 list | grep virtualenv # Create a project directory Make a directory where we can try virtual environment. Then `cd` into it. Replace `<PROJECT>` with a name of your choosing: $ mkdir <PROJECT> $ cd <PROJECT> # Create `venv`, a virtual environment To create a virtual environment in your `<PROJECT>` directory, use `virtualenv` followed by `-p /usr/bin/python3` to specify the python version you want to use. Finally, specify the name of your virtual environment as `venv`. $ virtualenv -p /usr/bin/python3 venv If you only have one version of python, you can be less verbose and use: $ virtualenv venv Just be aware that when you don't use the -p flag, virtualenv will default to the active python, which usually means the system python. This practice is still useful even when you only have one version of python on your system. This is because the system python depends on certain versions of packages, and you can run into situations where older version don't work with your new package installs and you won't be able to update the system packages. Virtualenv circumvents this because you can install any version you want into it's env space. # Activate `venv` You activate your virtual environment using the following command: $ source venv/bin/activate Once your virtual environment is active you should see the name on your terminal as such: (venv) $ # Check your virtual environment to make sure it's got all the right stuff (venv) $ which python (venv) $ which pip (venv) $ python --version # Play with packages within your virtual environment (venv) $ pip install numpy (venv) $ pip install django (venv) $ pip install pandas (venv) $ pip list # Freeze your required software Freezing your requirements allows you to move around your software while being able to track the required software, and easily installing your it using `pip`: (venv) $ pip freeze --local > requirements.txt To then install this required software using the `requirements.txt` file, run: (venv) $ pip install -r requirements.txt # Deactivate your `venv` All you have to do is: (venv) $ deactivate And the `(venv)` should disappear. # Alternative: install `virtualenv` with `python2.6` $ virtualenv -p /usr/bin/python2.6 VENV_26 $ ls $ source VENV_26/bin/activate $ which python $ python --version Everything else should be the same as for `python3`.

COMMENTS


Here is virtualenv for Windows: https://www.inertia7.com/projects/75

back to all projects