Skip to content

Instantly share code, notes, and snippets.

@calumroy
Last active May 19, 2022 07:51
Show Gist options
  • Save calumroy/4e0f83d99e02f23b697a to your computer and use it in GitHub Desktop.
Save calumroy/4e0f83d99e02f23b697a to your computer and use it in GitHub Desktop.
Virtual Envs, Unit Testing, IPython

Python3 virtual env soution

Start virtual python environment.
pipenv shell
Install requirments from a Pipfile
pipenv install

example project
New project "switch-boat" imported from switch-pi
use pipenv to install requirements from PipFile.
I also had to install python 3.7 with pyenv
pyenv install 3.7.13
I had to uninstall virtualenv since pipenv replaces it.
pip uninstall virtualenv
ip install pipenv
In the switch-boat repo use
pipenv install
Install the new virtualenv fromt he PipFile
pipenv install
Start the virtual env
`pipenv shell
In vscode install the "Python environmetn manager" now select pipenv switch-boat environment terminal

Outdated virtual environment solution

use virtaulenv wrapper

  • pyenv global 3.7.3 Switch to python version 3 or larger
  • sudo pip install virtualenvwrapper install virtualenv wrapper
  • mkvirtualenv env_name make the python virtual environment
  • workon env_name enter the virtual env
  • pip freeze > requirements.txt Create a text file of all the python libs
  • pip install -r requirements.txt install from that list

Python3 virtual environment use venv

Install with
sudo apt-get install python3-venv

To make a new virtual env use
python3 -m venv tutorial-env
A common directory location for a virtual environment is .venv

To source the environment use
source tutorial-env/bin/activate

pip

install requirements from a requirements.txt

    pip install -r requirements.txt

Show the version of a pip installed package. This is useful to add to requirements.txt

    pip show django-filter

requirements.txt might look like this

Automatically create a requirments.txt file for a virtualenv

    pip freeze > requirements.txt
ipdb==0.8
ipython==1.1.0
mock==1.0.1
newrelic==2.4.0.4
nose==1.3.0 

To automatically activate a virtual env when entering a directory containing a .env file use autoenv On OSX (you need to have installed homebrew)

     brew install autoenv  

On Linux

git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv  
echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc

Mock

pip install mock  

Nose

pip install nose

Nose is a unit tester. Create a test class and test functions for nose to run eg:

class TestExampleTwo:
    def test_c(self):
        assert 'c' == 'c'  

Here, nose will first create an object of type TestExampleTwo, and only then run test_c: Run the tests with,

$ nosetests
.
----------------------------------------------------------------------
Ran 1 test in 0.003s

To run a particular and prevent nose from capturing the output run the following

nosetests --nocapture test_file_name.py:test_class_name.test_case_name  

Most new test functions you write should look like either of these tests -- a simple test function, or a class containing one or more test functions.

def test():
    setup_test()
    try:
        do_test()
        make_test_assertions()
    finally:
        cleanup_after_test()

Readline

    easy_install readline

iPython

    pip install ipython

ipdb

    pip install ipdb

This is a Python debugger. To set a break point with ipdb add the following to your python code:

    import ipdb; ipdb.set_trace()
  • c continue
  • s step into
  • n step over
  • unt continue until a line below this is reached
  • p print the value of an expression
  • ! execute the statement
  • l list the 10 lines around the current position

To Install and use different python versions in a virtualenv

An easy solution is to use pyenv

brew update
brew install pyenv

To list availiable python versions to install

pyenv install --list

To install a verion e.g python 3.5

pyenv install 3.5.0

To set a local application-specific Python version.

pyenv local 3.5.0
pyenv rehash

To set the global python version in all shells by writing the version name to the ~/.pyenv/version file. This version can be overridden by an application-specific .python-version file, or by setting the PYENV_VERSION environment variable.

pyenv global 2.7.6
pyenv rehash

List all Python versions known to pyenv, and shows an asterisk next to the currently active version.

pyenv versions

To create a new virtualenv with a pyenv version of python set the local version of python with pyenv then create the virtualenv and use the pyenv version of python.

pyenv local 3.5.0
mkvirtualenv -p `pyenv which python` n_note_py2mk

Jupyter Kernel with virtualenv

To use a virtualenv and get jupyter notebook using the python version form the virtualenv activate the virtualenv and then install jupyter within the active virtualenv

(n_note_py2mk)$ pip install jupyter

jupyter comes with ipykernel, but somehow you manage to get an error due to ipykernel, then for reference ipykernel package can be installed using:

(n_note_py2mk)$ pip install ipykernel

Next, set up the kernel

install --user --name n_note_py2mk --display-name "n_note_py2mk"

then start jupyter notebook (the venv need not be activated for this step)

(n_note_py2mk)$ jupyter notebook

or

jupyter notebook

Virtualenv is old and deprecated

Virtualenv Install

pip install virtualenv

To set up a new Python environment, move into the directory where you would like to store the environment, and use the virtualenv utility to create the new environment.

virtualenv venv

To use an environment, run

source venv/bin/activate

Your command prompt will change to show the active environment. Once you have finished working in the current virtual environment, run deactivate to restore your settings to normal.

Virtualenv Wrapper

A set of command line tools to virtualenv can be installed to make using virtualenv more enjoyable.

pip install virtualenvwrapper
...
export WORKON_HOME=~/Envs  
mkdir -p $WORKON_HOME  
source /usr/local/bin/virtualenvwrapper.sh  
mkvirtualenv env1  

Usage

Create a virtual environment:

mkvirtualenv venv

Work on a virtual environment:

workon venv  

Deactivating is still the same:

deactivate

List all of the environments:

lsvirtualenv

to delete a virtual env. Just deactivate and delete the folder in ~/.virtualenvs/ or

    (mynewenv)$ deactivate
rmvirtualenv mynewenv
@calumroy
Copy link
Author

calumroy commented Mar 26, 2019

Conda Enviroments

Check Conda is installed

conda -V

Update

conda update conda

Create a virtual environment for your project

conda create -n yourenvname python=x.x

List conda environements

conda env list

Activate your virtual environment.

conda activate yourenvname
# This is deprecated source activate yourenvname

Install additional Python packages to a virtual environment.

To install additional packages only to your virtual environment, enter the following command where yourenvname is the name of your environemnt, and [package] is the name of the package you wish to install. Failure to specify “-n yourenvname” will install the package to the root Python installation.

conda install -n yourenvname [package]

Deactivate your virtual environment.

conda deactivate yourenvname
# This is deprecated source deactivate

Save all your requirements

conda list -e > requirements.txt #Save all the info about packages to your folder

Add the conda python kernel to jupyter

python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment