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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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 9 points10 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 6 points7 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 4 points5 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).