Why no longer Conda
I used miniconda to manage python virtual environment for several months, but I feel:
Conda does not have a nice integration in Doom Emacs
Conda is slow to manage packages
How to install Pipenv
Pipenv is another tool to create virtual environment with Pipfile. It also allows me to specify python version when creating the environment, but there are several things to be careful about during the installation, especially when integrating it with Doom Emacs. Here are the installation steps on Ubuntu 20.04:
Install pipenv with the system pip
pip install --user pipenv
Install pyenv with
curl https://pyenv.run | bash
and add the following lines to~/.bashrc
and don’t forgetsource ~/.bashrc
afterwards. Pyenv is used to install and manage different python versions.
1
2
3
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Install the python version with
pyenv install 3.10.9
before creating the environment. Don’t usepyvenv --python 3.10
because if there is issues with the python installation, the error won’t be promted. For instance, when I used the commandpyvenv --python 3.10
to create an environment and activate it withpipenv shell
in the project directory, I could not install any package withpipenv install
due to some SSL certificate error (and I can’t usepip install
neither in the environment because my system python version is different). It took me quite a while to realize that the problem might occur in my python installation (as the package installation was only successful in the system environment). It was only when I uninstalled python and reinstalled withpyenv install
, I was reported the missing library error, and after installing the related libraries, the problem was resolved.Create the project directory.
Inside the project directory, run
pipenv --python 3.10
, this will create the specified environment andPipfile
in the directory. Then runpipenv shell
to activate the environment.To install any package, use
pipenv install <package>
. Note that when there was an installation failure reportingFailed building wheel for ...
, trypipenv install setuptools
andpipenv install wheel
first, this solved my problem.
Integration with Doom Emacs
I am still very very new to Emacs so I don’t know how to write config files to improve my workflow. But with the Pipfile listed the lanauge server should be able to recognize the python file, otherwise can use pipenv-python
to specify the python version.
An important thing to note is the language server should be also installed in the environment with pipenv install python-lsp-server
as well as related formatter pipenv install flake8
and pipenv install black
, otherwise emacs cannot resolve the installed package import.