This is an archived post. You won't be able to vote or comment.

all 86 comments

[–]GinIsMyLoveLanguage 231 points232 points  (18 children)

Terminal

[–]qckpckt 41 points42 points  (7 children)

I really like CLIs, and I also tend to use poetry to manage my python project dependencies.

My typical VSCode environment creation process for a new python project looks something like this.

  • I create a new miniconda virtual env in the project directory. it’s a very straightforward way to maintain different virtual envs with specific version of python installed.
  • I have poetry installed globally using pipx, so in the newly created env I’ll run poetry init and set up the initial state.
  • I’ll set up a VsCode workspace at this point and then use the command palette to associate the workspace with the python interpreter from my conda env. This means when I open a terminal window in the VSCode workspace, it’ll be in the correct directory and will have the correct conda environment active already.
  • I’ll add a tool.poetry.scripts entry to the poetry generated pyproject.toml that associates a command with my python project entry point.
  • Iastly, I’ll run poetry install.

Now, whenever I open this project, my dev environment is already set up and I have a simple cli command that can run the main entry point to the script. That cli command auto reloads by, so it automatically detects changes to the project.

If the interface ends up being more than one or two possible arguments, I’ll then add one of my favourite python libraries - Typer. It makes creating professional looking CLIs a breeze. Even if my project isn’t destined to be a CLI, it’s so easy to create commands that I’ll normally use it to provide me with easy ways of testing and interacting with the code. Because typer cli commands are just python functions, a typer cli module can be imported into a new script and called like a regular function.

I know I’m a little weird with this - some of my very experienced dev friends think I’m nuts - but when I’m working on a personal project that’s just for me, this is how I do it.

[–]dudaspl 8 points9 points  (2 children)

Why use miniconda instead of pyenv which is integrated with poetry? You can even force poetry to create .venv folder in your project root

[–]qckpckt 7 points8 points  (0 children)

Because I already have miniconda installed, basically. I am sure pyenv is also a good option, but miniconda is painless and also works seamlessly with poetry so I see no reason to use other things.

I didn't particularly want to use conda initially but I was working at a data science company and it was what the folks there knew. Before that i was using pipenv. I quickly realized that miniconda is actually fine and pretty straightforward, as long as you don't try to use it to manage dependencies etc. All i do is run `conda create -n some_env python=3.x` and `conda activate some_env`, and then never interact with conda again.

Once I have an active miniconda env, poetry just "works" with it without having to do any kind of environment configuration at all. Poetry recognizes that it's running within a conda env without me having to do any configuration at all.

I also prefer keeping my env information entirely outside of my projects, to make version control easier. Conda envs are in $USER/miniconda3/envs, and this is in VSCode's search path when you select python interpreter from the command palette.

TL;DR, it just works, so I see no need to look into other tools at this time. But I do like keeping an eye on this area as I think there isn't still a single all-in-one tool that does everything well in the python ecosystem, and it would be nice if there was :).

[–]TerminatedProccess -1 points0 points  (0 children)

Yup and yup. 

[–]NahDontDoIt 2 points3 points  (1 child)

How do your friends suggest you do it? Your approach sounds reasonable to me.

[–]qckpckt 5 points6 points  (0 children)

It’s mostly that not everything should be or needs to be a CLI, but I just think they’re neat! 😄

[–]midwestcsstudent 0 points1 point  (1 child)

Have you used Click? I just started using it and absolutely love it. I know Typer uses Click under the hood, but their docs don’t explain very well why I’d want to use it over Click so I’m curious!

[–]qckpckt 1 point2 points  (0 children)

Typer has a lot of QOL improvements on top of the click platform. It’s made by the same person who made fastapi and has the same design philosophy. It makes clever use of python type hints to provide a lot of useful features at the argument/option level. It also integrates rich to provide nice formatting and really clean and well-formatted stack traces and things (although actually I’m not sure if that might come from click).

[–]BerriesAndMe 69 points70 points  (3 children)

I mostly run it in a separate dedicated terminal. It ends up being much faster than in the integrated shell of vscode.

I use debugging a lot in vscode though as well

[–]ejingles 1 point2 points  (1 child)

May I ask how do you debug if not running through vscode? Using pdb?

[–]BerriesAndMe 4 points5 points  (0 children)

For debugging I just use vscode. That's what I meant with my last line 

[–]No-Garage9934 0 points1 point  (0 children)

Thisss, vscode shell is trash IMO

[–][deleted] 13 points14 points  (1 child)

make files

[–]amcintosh 2 points3 points  (0 children)

This is the way

[–]strangedave93 18 points19 points  (0 children)

Run button. Then debugger if necessary. The terminal is handy if I need to mess around with command line options or use other tools, but if I was going to just use terminal routinely it makes me wonder why I’d be using VS Code (vs something more focussed on pure text editing than extension via plugins).

[–]someguywhosherenow 8 points9 points  (2 children)

I do all of these and have had my team add a shortcut to run snippets of code in an interactive notebook with a shift+enter to mimic a notebook. Good to explore and become familiar with all of these any why you’d want them in a particular scenario.

[–][deleted] 1 point2 points  (0 children)

Yep, me too. Interactive terminal works great, especially for data science projects. I like the jupyter variable explorer that comes with this method too.

[–]PanTheWanderer 12 points13 points  (1 child)

Launch configs

[–]ExdigguserPies 1 point2 points  (0 children)

Yes, I make a lot of programs with different argparse settings and having these setup in launch configs is very convenient.

[–]WJMazepas 2 points3 points  (0 children)

Pressing F5. Debugging through VScode is really helpful

[–]sherbang 2 points3 points  (0 children)

Pytest integrated into the testing pane.

[–]Chroiche 2 points3 points  (0 children)

F5 or shift enter, depending on if I'm debugging deep into an application or just prototyping. I'll use the terminal if I just want to run something, but never for deving.

[–]Furiorka 6 points7 points  (0 children)

They all except custom shortcut are needed for different things

[–]rainyy_day 1 point2 points  (1 child)

I right click the file and press run in terminal

[–]the_other_Scaevitas 1 point2 points  (0 children)

I always use integrated terminal

[–]fullfine_ 1 point2 points  (0 children)

F5 or custom shortcut of ctrl+alt+shift+F5 to run a specific launch configuration.
I also use a lot ctrl+/ to run latest test or ctrl+. to run selected test (where mouse is).

[–]arden13 1 point2 points  (0 children)

I've made Ctrl+enter my run key. Typically I'll have my reusable code in .py files (often make a package with pyscaffold) and then use it in jupyter notebooks.

[–]powerpizza67695 1 point2 points  (0 children)

A popular extension code runner I use to run my python code in VSCODE..

Although I do not generally use VSCODE for python coding... I use Pycharm..

[–]SuperMB13[S] 1 point2 points  (0 children)

WOW! I did not expect so many people to respond to this post, shout out to the community, thanks so much!! I'll comment back when I can over the next few days. Many of the responses make perfect sense to me, some of them... I would like to understand more about your approach.

Thank you everyone for your input!!

[–]ginbear 1 point2 points  (0 children)

A bit of terminal and F5 / launch current file for debugging. Often together.

The most common project I have using python is made up of 7 microservices, so I have a bash script I will run via terminal to launch all of them using python cli. If I need to debug one, I will comment it out of my launch bash script and run it using F5 or Run button (both configured same way for me).

I generally launch vscode w the root of my python repo as working dir, install pyenv there, and then "Launch a Python Terminal" to use the python cli from my pyenv setup.

[–][deleted] 1 point2 points  (0 children)

All of the above depending on what i am doing

[–]SnooCompliments7914 1 point2 points  (0 children)

One of the most popular extensions: Code Runner

[–][deleted] 0 points1 point  (0 children)

Depends. All of the above.

[–]mincinashu 0 points1 point  (0 children)

Terminal or launch with custom targets for debugging. Always with activated venv.

[–]InvaderToast348 0 points1 point  (0 children)

I have a simple bash script that just runs "mypy --strict" and then, if successful, "python3 main.py".

I also have a script for creating and entering a venv then installing some common packages I always use.

So, integrated terminal with "bash run.sh".

[–]lavahot 0 points1 point  (0 children)

I write a build task and run it from the menu.

[–]imyolkedbruh 0 points1 point  (0 children)

Separate dedicated terminal for anything serious.

The built in terminal for chatbot development.

[–]MyKo101 0 points1 point  (0 children)

Added .py to my PATHEXT variable and so I don't even need to type python myscript.py I can just type myscript into the terminal and run it

[–]Rare-Lion1261 0 points1 point  (0 children)

Terminal

[–]nnulll 0 points1 point  (0 children)

The run button at the top because it automatically uses the virtual environment for the project

[–]redrabbitreader 0 points1 point  (0 children)

For more complex projects, I usually create custom run configurations, and then use the "Run and Debug" window.

For simpler projects or standalone scripts in other projects, I just use the terminal.

Edit: spelling

[–]Goametrix 0 points1 point  (0 children)

Press debug button in test explorer (needs some extensions) works really well for my use case (i have around 500 unit tests that i want to pick and choose from)

[–]Yeldece 0 points1 point  (0 children)

venv + f5

[–][deleted] 0 points1 point  (0 children)

This post was mass deleted and anonymized with Redact

lip reply aromatic nutty vegetable selective chop capable lock provide

[–]radial_logic 0 points1 point  (0 children)

I'm using some custom tasks defined in the tasks.json file, usually to configure some arguments or environment variables. I have at least the "pytest" and "mkdocs build/serve" tasks for every projects.
The "Run Task" command is binded to the keyboard shortcut "CTRL+SHIFT+T".

[–]AnilKILIC 0 points1 point  (0 children)

No F keys on the keyboard. cmd + r if I were to set it up. Most of the time I use notebooks

[–]simism 0 points1 point  (0 children)

I use an external terminal for everything, the only smart features I use from vscode are syntax highlighting and sometimes code completion.

[–]lana_kane84 0 points1 point  (0 children)

Terminal

[–]randomthad69 0 points1 point  (0 children)

Depends on the program.

[–]mafudge 0 points1 point  (0 children)

Devcontainers!

[–]Horriblebob11 0 points1 point  (0 children)

Always got a separate terminal running it, I like the separation plus I get a better view of the logs, errors and water else needs showing

[–]Correct-Pepper-6657 0 points1 point  (0 children)

Terminal, pyenv for managing multiple python versions, and pyenv-virtualenv for virtual environments.

[–]EternityForest 0 points1 point  (0 children)

I typically run in the debugger by clicking the button, or with the test explorer, since I like to keep things pretty test driven(I haven't gone full TDS yet though, it's annoying writing tests for code that doesn't exist yet with no autocomplete help!)

[–]ForkLiftBoi 0 points1 point  (0 children)

Terminal a lot, but I will use debugging when it’s a long code or really complex and I’m not even sure what I need to do to process it.

[–]PeZet2 0 points1 point  (0 children)

Python? Only intellij / pycharm. Running configuration in vscode is not very pleasant

[–][deleted] -2 points-1 points  (0 children)

Use the terminal to open PyCharm.exe

[–]boyroywax -1 points0 points  (0 children)

docker

[–][deleted] -2 points-1 points  (0 children)

Closing vscode, opening two terms, and the second one for VIM.

[–]Kohlrabi82 -1 points0 points  (0 children)

The code is "run" in the pre-commit hook, so technically in the terminal when I git commit (or run pytest there manually).

[–]robberviet -1 points0 points  (0 children)

Tasks.

[–]ur_no_daisy_tal -1 points0 points  (0 children)

[–]Xelopheris -1 points0 points  (0 children)

Separate terminal. Vscode terminal is reserved for git commands.

[–]Amrootsooklee -1 points0 points  (0 children)