Django + Django Rest Framework 实践《Django By Example》

Setting up Django in WSL with VS

2019-09-26  本文已影响0人  思考的虫子

Updated: 26 Sep 2019
Windows 10 Pro | WSL: Ubuntu 18.04

Ubuntu / WSL

Install Oh-My-Zsh

  1. install zsh, refer here.

    sudo apt-get update
    sudo apt upgrade
    
    sudo apt install zsh
    sudo apt-get install powerline fonts-powerline
    
  2. install oh-my-zsh

    Double check the official site, then run:

    sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
    

    Don't forget

    # Load pyenv automatically by adding
    # the following to ~/.bashrc:
    
    export PATH="/home/hustmck/.pyenv/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
    

Install Python with apt

  1. List all Python Version

    ls /usr/bin/python*
    
  2. Uninstall Old Python Optional

    sudo apt autoremove python
    
  3. Install Python

    sudo apt install python3.7
    
  4. Add soft link

     sudo ln -s /usr/bin/python3.7 /usr/bin/python3
    
  5. Add to PATH

    # ~/.zshrc  if using zsh
    export PATH=$PATH:/usr/bin/python3.7
    
  6. Test

    python --version
    

Install via pyenv

Install Python

# you can check available versions
pyenv install — list

# install python
pyenv install 3.7.2

# set default version
pyenv global 3.7.2

# check using python version
pyenv versions

If success, go to next step Install pipenv:

# if see "ImportError: No module named '_ctypes"
sudo apt-get install libffi-dev
    
# if "configure: error: no acceptable C compiler found in $PATH"
sudo apt-get install build-essential
    
# or install below directly
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

Install pipenv

pip install pipenv

Clone Repository

git clone xxx.git
# Go to the folder where Pipfile located, for me it's "proj"
cd proj

Install Virtual Environment

pipenv install

If success, Go to Start VSCode

if see something like:

# OSError: [Errno 8] Exec format error: '/mnt/c/Users/hustm/AppData/Local/Microsoft/WindowsApps/python.exe'

Then add the python path parameter

which python3
/home/ck/.pyenv/shims/python
pipenv install --python=/home/ck/.pyenv/shims/python

if install dev packages

pipenv install --dev --python=/home/ck/.pyenv/shims/python

Start VSCode

code .

Make sure WSL extension: Python already installed

Edit or add

# .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Windows: Django",
            "type": "python",
            "request": "launch",
            // change below if your manage.py in different location
            "program": "${workspaceFolder}\\proj\\manage.py",
            "args": [
                "runserver",
                // "--noreload"
            ],
            "django": true
        },
        {
            "name": "WSL: Django",
            "type": "python",
            "request": "launch",
            // change below if your manage.py in different location
            "program": "${workspaceFolder}/proj/manage.py",
            "args": [
                "runserver",
                // "--noreload"
            ],
            "django": true
        }
    ]
}

Edit (set font Family to show invisible characters)

.vscode/settings.json
{
    "terminal.integrated.fontFamily": "Meslo LG M DZ for Powerline",
    // can use 'pipenv --venv' to find your own path
    "python.pythonPath": "${workspaceFolder}/proj/.venv/bin/python",
    "python.linting.flake8Enabled": true,
    "python.linting.enabled": true
}

Done


1569095143155.png
上一篇 下一篇

猜你喜欢

热点阅读