前言
之前用过Anaconda下的Spyder、Pycharm和Jupyter等写过python的数据分析项目,各有优劣。因为我的C++和Golang项目都是使用的VSCode(CLion、Golang和Pycharm都需要付费使用,而且在Mac上消耗资源比VSCode更大),所以索性将Mac和Windows的python项目都迁移到VSCode上。
安装vscode的python插件
![13946199-957e63789cc8d394.png](https://i-blog.csdnimg.cn/blog_migrate/2e6ceff0af5a66b1986db575e33f2136.png)
python插件
选择python解释器
ctrl+shift+P
打开vscode
的命令行,输入python: select Interpreter
选择合适的python版本。
![13946199-7680d060af159fea.png](https://i-blog.csdnimg.cn/blog_migrate/f1af91fcd6c4e65e34c393b3e679887a.png)
选择解释器
可以看到在.vscode/launch.json
中,python的安装目录已经加入,和用户的全局设置区分开:
{
"python.pythonPath": "C:\\Users\\YANG\\AppData\\Local\\Programs\\Python\\Python37\\python.exe"
}
运行代码
现在我已经写好了一个小项目,并使用git
托管,在main
函数下,首先确保文件组织格式正确:
![13946199-11d74eabed5cb02c.png](https://i-blog.csdnimg.cn/blog_migrate/4302f80a4d5e4fcd18b00581dbd63911.png)
工程组织格式
在windows
下你可以直接使用命令行运行项目:
cd ./src
python test.py
![13946199-b644a3edb57b63d2.png](https://i-blog.csdnimg.cn/blog_migrate/779ec176be35b0ae36a942829f55bd49.png)
命令行运行
在vscode
中,只需要右键点击运行代码即可运行工程代码,本质上仍然是调用环境变量中的python.exe
执行对应的python
文件。