Bottom Page

Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 VS Code from start
#1
This will be about VS Code.
Will take if start,so get Python up running with some advice that make it easy to run code.
I has quick review before to look at here.
That also show some basic stuff about the editor.

Quote:Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS and Linux
They have but a lot of resources to make Python work well in there editor.
Like hire @Don Jayamanne the maker of Python Extension for VS Code.
VS Code is a multi-language editor that is same league as Atom and Sublime Text.

Start
Should make sure that python and pip work from command line,before install.
This will make setup in editor and stuff like virtual environment easier.
Python 3.6/3.7 and pip installation under Windows
C:\code
λ python -V
Python 3.7.0

C:\code
λ pip -V
pip 18.0 from c:\python37\lib\site-packages\pip (python 3.7)
Linux if python3 point to Python 3 use that in setup later.

Install
Install choose user installer.

After install the first ting to do is install needed Extension.
Here list of needed and some useful Extension.
Can also use Marketplace to find and install extensions.
  • python
  • Python Extension Pack
  • Code Runner
  • vscode-icons
  • Bracket Pair Colorizer
  • highlight-icemode
  • PlasticAB # My Theme

Setup
Now gone do some setup to Code runner and install some stuff so PEP-8 format of code and linter work.
İmage

Search in setting code-runner.executorMap
Click on the pen so on right side(user setting) so this is the result.
"php": "php",
"python": "$pythonPath $fullFileName",
"perl": "perl",
This make Code runner button always use Python interpreter down in left corner(see image).
Can click on to change to other python interpreter installed like eg Anaconda.
Search setting code-runner.runInTerminal and set it to True,make stuff like input() work in Code runner.

Install of modules command line for format code and linter.
pip install autopep8 yapf black
pip install pylint
Search setting python.formatting.provider to choose formater.
Command platte search pylint to turn on/off,it can be annoying to have on alle time.

Run Code
İmage


Start VS Code from any folder,eg for a virtual environment
With code . from command line in any folder will open files in that folder in VS Code.
Example with virtual environment that build into Python venv
# Make
E:\div_code
λ python -m venv my_env

# cd in
E:\div_code
λ cd my_env

# Activate
E:\div_code\my_env
λ E:\div_code\my_env\Scripts\Activate

# Test pip
(my_env) E:\div_code\my_env
λ pip -V
pip 10.0.1 from e:\div_code\my_env\lib\site-packages\pip (python 3.7

# Install required package 
(my_env) E:\div_code\my_env
λ pip install requests
Collecting requests

# Start VS Code
(my_env) E:\div_code\my_env
λ code .
See now that it automatically find Python interpreter in virtual environment.
So push run button it will use Python version in virtual environment.
İmage


This was basic setup will post more stuff later.
j.crater, metulburr, Larz60+ And 1 others like this post
Quote
#2
Look at some useful features.

Format code
Before i did mention to install:
pip install autopep8 yapf black
Take a look at format code with the new star Black.
So right click and Format Document,after have choose "python.formatting.provider": "black"
Here a image on how it work have a long line with dict,list and tuple,right side after format.
İmage

Mess up some code before and after format.
Messed up:
import random
import time

class Coin:
    def __init__(self ):
      self.sideup="Heads"

    def toss(self):
            if random.randrange( 2)== 0: 
              self.sideup =  "Heads"
            else:
              self.sideup= "Tails"

def toss_result(  ):
  my_coin=Coin( )
  print(f"This side is up: {my_coin.sideup}")
  print("I am tossing the coin...")
  time.sleep(4)
  my_coin.toss()
  print(f"This side is up: {my_coin.sideup}")

if __name__ == "__main__" :
  toss_result( )
After format:
import random
import time

class Coin:
    def __init__(self):
        self.sideup = "Heads"

    def toss(self):
        if random.randrange(2) == 0:
            self.sideup = "Heads"
        else:
            self.sideup = "Tails"

def toss_result():
    my_coin = Coin()
    print(f"This side is up: {my_coin.sideup}")
    print("I am tossing the coin...")
    time.sleep(4)
    my_coin.toss()
    print(f"This side is up: {my_coin.sideup}")

if __name__ == "__main__":
    toss_result()

IntelliSense
One of the strong feature with editor is IntelliSense
It goes beyond auto completion.
You can view available methods, parameter hints, short documentation, source code(where is function/method source code) etc...
If take mouse over get() method:
İmage

Need to scroll method doc or that it stay open ctrl+space.
Now can also look at source code for this get() function alt+f12(peek).
F12 goes to source code where get() is defined.

Linter
You annoying friend pylint Wall
pip install pylint
So i turn it of in normal use,then just turn it on if want a check of code.
Command Palette ctrl+shift+p search Pylint then get an option on/off.
So how dos it work:
İmage

What it finds is down in left corner errors and Warnings .
Click on it and it show up in windows under.
So the fix is pretty obvious missing {.
print(f"This side is up: {my_coin.sideup}") 
Save and this error/advise goes away.
adt, Larz60+, metulburr like this post
Quote
#3
Gone look at VS Code in Linux,using Linux Mint 19 “Tara” Cinnamon .
As this is from start gone look a little of setup for Python 3.
Install just gone use sudo on all for now,gone look setup pyenv in a later post.
sudo apt install python3-pip
sudo apt-get install python3-setuptools
sudo pip3 install autopep8 yapf black
sudo pip3 install pylint
A fix to get pip 18.0 to work,after pip3 install --upgrade pip.
sudo nano /usr/bin/pip3
# Then change to this
from pip._internal import main
See that 2.7.15 and 3.6.5 are default versions for Mint 19.
tom@tom-VirtualBox:~$ python -V
Python 2.7.15rc1
tom@tom-VirtualBox:~$ python3 -V
Python 3.6.5
tom@tom-VirtualBox:~$ pip3 -V
pip 18.0 from /home/tom/.local/lib/python3.6/site-packages/pip (python 3.6)
tom@tom-VirtualBox:~$ 

Install VS Code
Open deb in package installer(default option).
Or download and from command line:
sudo dpkg -i code_version.deb
Setting up VS Code is the same as in first post.
Remeber this in setting,so code runner always follows python version down in left corner
"python": "$pythonPath $fullFileName",
A image of edtior running,it will start with version 2.7.15 in left corner just click and change to 3.6.5.
İmage
Larz60+ likes this post
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)