Skip to content

Instantly share code, notes, and snippets.

@NdagiStanley NdagiStanley/.md
Last active Aug 4, 2019

Embed
What would you like to do?

flake8

  • Usage:

    To skip file enter this comment in that file, preferably at the top.

    # flake8: noqa

    To not issue warnings, add the following comment at the end of the file.

    # noqa
    # or
    # noqa: <error-token>
    # or
    # noqa: <error-token>, <error-token>...
  • VS Code

    "python.linting.flake8Enabled": true,
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Args": [
        "--max-line-length=200",
        "--ignore=D,E111,E127,E128,E225,E501,E731,E731,E901,F403,F405,W291,W293,W391"
    ],

PySnooper

  • Usage:
    import pysnooper
    
    @pysnooper.snoop()

Installing packages

There are differences in installing packages in the two versions of Python

For python version 2:

python -m pip install pyfirebase

or if you updated the shell command:

python2 -m pip install pyfirebase

For python version 3:

pip install pyfirebase

Different versions of Python

Using pyenv

brew install pyenv
pyenv install <version>. For the full list, run pyenv install --list

Always use virtual environments

Virtualenv

For a specific directory set python version: pyenv local <version>
mkvirtualenv --python=$(pyenv which python) <env name>

Then use workon (that comes with virtualenv-wrapper)

python2 and python3 shall be used to specify Python version 2 and Python version 3 respectively.

Download & install

Download from the Python website and install.

Add to Path

  • Click on Add to Path in the installation windows, otherwise:

  • Navigate to My Computer (System)/ Properties/ Advanced Settings/ Environment Variables and add c:\Python27\ (for Python2) and/or c:\Python36\ (for Python3)

Update shell command to call python

If you are one version of Python skip this part. Otherwise, I would advise to update the less used python version as described below:

For:

python2
  • Navigate to the Python27 folder in C:\ and change python.exe to python2.exe.
python3
  • Navigate to the Python32-36 (or otherwise) folder in C:\ and change python.exe to python3.exe

PIP (package installer for Python)

Python3 comes with pip installed.

Install pip in python2

python get-pip.py

or if you updated the shell command:

python2 get-pip.py

Installing packages

Virtual Environments in Windows

Virtual environments

More details are here featuring the three main ways to create and use virtual environments in Python:

  • pipenv
  • virtualenv
  • virtualenv-wrapper

and bonus:

  • venv (Avaliable in python 3.3+)

pipenv

# First install pipenv package:
<pip install pipenv>

cd project_folder
pipenv install <package>
pipenv run <script>
pipenv shell  # Shell (Anthing run after this is done within the environment)

venv (Avaliable in python 3.3+)

python3 -m venv /path/to/new/virtual/environment

virtualenv

# First install virtualenv package:
<pip install virtualenv>

cd project_folder

# Create environment
virtualenv <environment-name>
# or
virtualenv </path/to/new/virtual/environment>

# Specify python version for the environment
virtualenv -p <python-version shell-command> <environment-name>
# or
virtualenv -p <python-version shell-command> </path/to/new/virtual/environment>

# Activate environment
source <environment-name>/bin/activate
# or
source </path/to/new/virtual/environment>/bin/activate

<pip install package>
deactivate

virtualenv-wrapper

# First install virtualenvwrapper package:
<pip install virtualenvwrapper>

export WORKON_HOME=~/Envs

source /usr/local/bin/virtualenvwrapper.sh
# or
source <path/to/virtualenvwrapper.sh>

mkvirtualenv <environment-name>
workon <environment-name>
deactivate
workon <previously-created-environment-name>
mkvirtualenv <previously-created-environment-name>

virtualenvwrapper-win

For Windows the package is virtualenvwrapper-win and WORKON_HOME is %USERPROFILE%\Envs by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.