Hacker News new | ask | show | jobs
by rihegher 1273 days ago
I have just started getting into python as I'm experimenting with pytorch and after a lot of googling I'm still trying to figure out how to use conda properly. Could anyone point me to the best way to manage python environments and packages?
8 comments

I don't think there is a "best" way, in my biased opinion package management for Python is terrible. It works for trivial cases but if you do anything serious it will get unmanageable fast.

Everyone will tell you of the "best" (i.e. their favorite) way to handle packages and dependencies. Usually mixed with some sandbox framework. Everything will be broken in some way: nonstandard, convoluted, too slow, cannot handle corner cases, or just way too difficult to understand. Even those labeled "for humans". Everyone will agree the situation is a mess "except for this one way that works": never trust that way, it doesn't really work past trivial cases.

Of course this is just my experience. But I feel after years of battling with Python and trying different things, I've earned the right to say this.

Tried tons of ways, and finally found peace of mind with poetry:

https://python-poetry.org/

It encourages to set up project specific definitions which are saved in the local pyproject.toml file. Keeping everything local and project specific, including the env definition, turns out to be a fantastic boon to reproducibility and sanity of mind.

Being able to just cd into a project folder, doing 'poetry shell' (I have it alised to 'psh'), and start developing, is so convenient.
Thanks! this is exactly what I was looking for.
Use mamba instead of conda to install your packages, and always use the conda-forge channel. mamba is essentially a drop-in replacement. I have always found conda to be horribly slow for installing packages. For environments:

- `conda env create|list|remove` commands to manage them

- `conda activate <env name>` to enter your environment

- `conda deactivate` to return to your base environment

If you want to use a conda environment in jupyter, you must install ipykernel and create a kernel definition for your environment:

    conda activate <env name>
    mamba install -c conda-forge ipykernel
    ipython kernel install --user --name <env name>
In practice I have found anacondas environment management and usage to be pretty seamless.
Not conda, but using the built-ins should work for most use cases.

---

1. Create a virtual environment named venv

  cd $PROJECT_FOLDER
  python -m venv venv
2. Activate it.

  source ./venv/bin/activate
(In vscode, Ctrl+P > Select: Python Interpreter)

3. Define and install dependencies

  nano requirements.txt
  pip install -r requirements.txt
(or just "pip install ___")
I always recommend the hyper modern guide to python. https://medium.com/@cjolowicz/hypermodern-python-d44485d9d76...
Thanks!
If you are using conda, use its environment management:

https://docs.conda.io/projects/conda/en/latest/user-guide/ta...

(Lots of the other advice here—mamba excepted, and that might be a good choice—is good for python in general but potentially will cause problems if you are otherwise using anaconda.)

I was a Poetry user then I discovered the joy of virtual environment-less PDM [1]. It's really easy to import your existing Poetry project in.

[1] https://pdm.fming.dev/latest/usage/pep582/

Pyenv for Python versions and/or environments, Poetry for package management and/or environments, and pip if you end up needing it for package management.