Skip to content

Instantly share code, notes, and snippets.

@NdagiStanley
Last active February 21, 2022 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NdagiStanley/bf9db623e8a96ef2ab631a28c9a1eba8 to your computer and use it in GitHub Desktop.
Save NdagiStanley/bf9db623e8a96ef2ab631a28c9a1eba8 to your computer and use it in GitHub Desktop.
All the Things Python
  • 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"
    ],
  • 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)

MAC (Unix) and Linux

Install Brew (on Mac, on Linux)

Install Python3:

brew install python

Download & install

Steps:

  1. Download from the Python website and install.
  2. 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:\Python36\

PIP (package installer for Python)

Python3 comes with pip installed.

Python 2 (deprecated) `python2` and `python3` shall be used to specify Python version 2 and Python version 3 respectively.
  1. Do Step 1

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
# https://https://gist.github.com/forkcs/76de0ea947a2fd27b131f9fa49bc1968
[metadata]
name = {name}
version = {version}
author = Fedor Soldatkin
author-email = fsoldatkin@yandex.ru
home-page = https://github.com/forkcs/{name}
description = {description}
long-description = file: README.md, CHANGELOG.md
long-description-content-type: text/markdown
license = APACHE 2.0
license-file = LICENSE
platform = any
keywords = {keywords}
classifiers =
Development Status :: 3 - Alpha
Intended Audience :: Developers
License :: OSI Approved :: Apache 2.0 License
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3.3
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Topic :: Software Development :: Libraries :: Python Modules
[options]
zip_safe = false
include_package_data = true
python_requires = >= 2.7, != 3.0.*, != 3.1.*, != 3.2.*
packages = {name}
test_suite = tests
setup_requires =
setuptools
# setuptools >=30.3.0 # minimal version for `setup.cfg`
# setuptools >=38.3.0 # version with most `setup.cfg` bugfixes
install_requires =
{install_requires}
tests_require =
{tests_require}
[options.extras_require]
dev =
docutils
Pygments
test =
green
coverage
ci =
# codacy-coverage
# codecov
[bdist_wheel]
universal = true
[check]
metadata = true
restructuredtext = true
strict = true
[sdist]
formats = zip, gztar
[coverage:report]
show_missing = true
exclude_lines =
pragma: no cover
if False
# @abc.abstractmethod
# @abc.abstractproperty
# raise NotImplementedError
# return NotImplemented
# except ImportError
[green]
file-pattern = test_*.py
verbose = 2
no-skip-report = true
quiet-stdout = true
run-coverage = true
[pydocstyle]
match-dir = (?!tests)(?!resources)(?!docs)[^\.].*
match = (?!test)(?!setup)[^\._].*\.py
inherit = false
ignore = D200, D203, D213, D406, D407 # Google conventions
[flake8]
max-line-length = 99
doctests = True
exclude = .git, .eggs, __pycache__, tests/, docs/, build/, dist/

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.

@AngieBil
Copy link

Thank you, I had a bit of difficulty using VSC.

@NdagiStanley
Copy link
Author

Thank you, I had a bit of difficulty using VSC.

You're welcome.

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