Skip to content

Instantly share code, notes, and snippets.

@mdbetancourt
Created October 15, 2017 20:12
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 mdbetancourt/01800309fd04b1027938a848cf8f8bbd to your computer and use it in GitHub Desktop.
Save mdbetancourt/01800309fd04b1027938a848cf8f8bbd to your computer and use it in GitHub Desktop.
Python3 (venv) programming with VS Code
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# dotenv
.env
# virtualenv
.venv
venv/
ENV/
.venv/
# Spyder project settings
.spyderproject
# Rope project settings
.ropeproject
# mypy
.mypy_cache
# for vscode
/.vscode/

Python3 (pipenv) programming with VS Code

  • OS : Windows 10

Prepare

  1. Install Python ( miniconda ) by scoop
  2. Install pip packages globally
    • pipenv
    • mypy
    • prospector
    • pylama
    • yapf

Install Texteditor

  1. Install VSCode
  2. Install Plugins
    • Python
    • MagicPython

Create Project

  1. Initialize
    $ # make project directory
    $ mkdir sampleproject
    $ cd sampleproject
    $ git init
    $
    $ # create virtual env
    $ pipenv --three  # initialize
    $ pipenv install flask
    $ pipenv install pytest==3.0.5 --dev
    $
    $ # enter .venv environment
    $ pipenv shell
    $ python -V # show the python version in venv
    $ exit
    $
    $ # create main.py
    $ touch main.py
    $
    $ # create module folder
    $ mkdir newmodule
    $ touch newmodule/__init__.py
    $ touch newmodule/calc.py
    $ 
    $ # execute
    $ pipenv run python main.py
    $
    $ # create test folder
    $ mkdir test

   $ touch test/init.py $ touch test/test_calc.py $ $ # testing $ pipenv run py.test $ $ # create setup.py if you want to publish to PyPI $ touch setup.py $ ```

  1. Download files from Gist and deploy

Deployment

  • project root /
    • .vscode /
      • launch.json
    • newmodule /
      • __init__.py
      • calc.py
    • test /
      • __init__.py
      • test_calc.py
    • .gitignore  - main.py
    • Pipfile
    • pipfile.lock

Run

  1. Run

    $ pipenv run python main.py
  2. Debug

You put launch.json and launch server

def add(x, y):
return x + y
def is_odd(number):
return number % 2 == 0
{
"version": "0.2.0",
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": ["WaitOnAbnormalExit", "WaitOnNormalExit", "RedirectOutput"]
},
{
"name": "Python Module",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"module": "module.name",
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": ["WaitOnAbnormalExit", "WaitOnNormalExit", "RedirectOutput"]
}
]
}
import sys
from newmodule import calc
def main(args):
assert args
for count in range(5):
print("{}. Hello World!".format(calc.add(count, 10)))
if __name__ == '__main__':
main(sys.argv)
import pytest
from newmodule import calc
@pytest.mark.parametrize(("x", "y", "expect"), [
(4, 8, 12),
(3, -3, 0),
])
def test_add(x, y, expect):
assert calc.add(x, y) == expect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment