您的位置:首页 > 编程语言 > Python开发

Python on VS Code

2016-05-06 23:38 330 查看

install python extension

Press F1, and input "ext install python".

Then the icon at the leftmost side of the status bar is saying that something is being installed.

Need to wait a while.

Use command "ext" + a space to see installed extensions.

use markdown as document

VS Code supports markdown files.

Ctrl+K, V : markdown: Open Preview to the side

Ctrl+Shift+V : markdown: Toggle Preview

create a python project

Select File -> Open Folder...

Create a tasks.json file under the .vscode folder in the project folder

Input below in the task.json file

// A task runner that runs a python program
{
"version": "0.1.0",
"command": "python",
"showOutput": "always",
"windows": {
"command": "python.exe"
},
"args": ["${file}"]
}


integrate with git

Make sure you have a repository on the server.

Install a git tool

Go to the project directory, open a command window, and run:

# initialize a git repository
git init

# set a remote with name origin
git remote add origin [https://github.com/<username>/<repository>]

# fetch the master branch files from the remote
git pull origin master


Start VS Code

Use the Git panel to work with the remote.

run a python file

Open the python file.

Press Ctrl+Shift+B.

debug

F9 : add/remove a breakpoint.

F5 or Ctrl + F5 : start debug

F5 : continue

F10 : step over

F11 : step into

Shift + F11 : step out

Ctrl + Shift + F10 : restart

Shift + F5 : stop

print Chinese words, but see question marks in the output console

The issue can be resolved by the following code:

import io
import os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')

Threading

Please see

http://www.tutorialspoint.com/python/python_multithreading.htm

Example: Execute at most 10 threads in a thread pool

from concurrent.futures import *
...
with ThreadPoolExecutor(max_workers=10) as executor:
for url in urls :
try:
i += 1
future = executor.submit(self.get_url, url)
future.add_done_callback(threadCallbackHandler(url).handle_callback)
except Exception as e:
print("Failed {0}. {1}".format(url, e))

# wait all threads finish.
executor.shutdown(wait=True)

class threadCallbackHandler:
def __init__(self, url):
self.url = url

def handle_callback(self, future) :
try:
if (future.exception() != None):
print("Failed {0}. Exception : {1}".format(self.url, future.exception()))
except CancelledError as e:
print("Cancelled {0}".format(self.url))
print("Done {0}".format(self.url))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: