<p>TL;DL: What I end up recommending is that you use pip-tools for your dependency management needs, and pyenv-virtualenv for your environment management needs. In the show I explain why you would want these things.</p>

<p>I talk about these tools:</p>

<ul>
  <li><a href="https://virtualenv.pypa.io/">virtualenv</a></li>
  <li><a href="https://python-poetry.org/">poetry</a></li>
  <li><a href="https://pipenv.pypa.io/">pipenv</a></li>
  <li><a href="https://github.com/jazzband/pip-tools/">pip-tools</a></li>
  <li><a href="https://virtualenvwrapper.readthedocs.io/">virtualenvwrapper</a></li>
  <li><a href="https://github.com/pyenv/pyenv">pyenv</a></li>
  <li><a href="https://github.com/pyenv/pyenv-virtualenv">pyenv-virtualenv</a></li>
</ul>

<p>I mention in passing, and as fodder for further shows:</p>

<ul>
  <li><a href="https://reproducible-builds.org/">reproducible builds</a></li>
  <li><a href="https://nixos.org/nix/">Nix</a></li>
  <li><a href="https://tox.readthedocs.io/">tox</a></li>
  <li>I also owe you a show on my awesome bash prompt that shows me which environment I am in.</li>
</ul>

<h2 id="install-pyenv-and-pyenv-virtualenv">Install pyenv and pyenv-virtualenv</h2>

<pre><code>git clone https://github.com/pyenv/pyenv ~/.pyenv
git clone https://github.com/pyenv/pyenv-virtualenv ~/.pyenv/plugins/pyenv-virtualenv
</code></pre>

<p>Add to <code>~/.bash_profile</code>:</p>

<pre><code>export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
</code></pre>

<p>Add to <code>~/.bash_profile</code> (optional):</p>

<pre><code>eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
</code></pre>

<p>The optional bits provide you with the <code>pyenv shell</code> functionality for setting a session-specific Python version, and automatic activation of the virtualenv. Most of the time you don’t need activation, scripts and commands run just fine via the shims, but some tooling around Python may sometimes need to know which virtualenv you’re in.</p>

<p>Run the export and eval lines in your shell to have the configuration work immediately. Alternatively, do <code>su - yourusername</code> to login to a new session that runs the profile. The <code>-</code> is important.</p>

<p>You might be able to get away with just opening a new tab or window in your terminal. Whether that runs the profile depends on your settings.</p>

<h2 id="set-up-your-pyenv-virtualenv-for-your-project">Set up your pyenv virtualenv for your project</h2>

<pre><code># Creates the virtualenv named my-project-env using 
# the python named system (your system default python)
pyenv virtualenv system my-project-env  
cd /path/to/my-project
pyenv local my-project-env
</code></pre>

<p>Your system Python may or may not work for this. You might have to install pip and virtualenv. It might still break with some message about <code>ensurepip</code> failing (currently both Nix (20.09pre) python and Ubuntu (18.04) python are failing for me, and older Anaconda pythons also had a broken venv). In that case, use pyenv to install a Python that works, and use that instead of the system python:</p>

<pre><code>pyenv install miniconda3-latest
pyenv virtualenv miniconda3-latest my-project-env
cd /path/to/my-project
pyenv local my-project-env
</code></pre>

<h2 id="install-pip-tools">Install pip-tools</h2>

<p>You’ll want to do this inside the virtual environment that you want to manage. Don’t install pip-tools globally.</p>

<pre><code>cd /path/to/my/project
# And, assuming you have the shims on your $PATH
# and you set the pyenv local as shown previously
python -m pip install pip-tools
</code></pre>

<p>Now put your requirements in <code>requirements.in</code>, one on each line, in the form you would give them to <code>pip</code> on the command line:</p>

<pre><code>somepackage &gt;=3, &lt;4
otherpackage &lt;7
</code></pre>

<p>Compile <code>requirements.in</code> to a <code>requirements.txt</code>:</p>

<pre><code>python -m piptools compile
</code></pre>

<p>You could run the shorter command <code>pip-compile</code> for convenience, but using the long form with <code>-m</code> looks it up through your configured Python, and makes it less likely for you to surprise yourself and run a tool in a different virtualenv than you expected. Same with <code>python -m pip</code> above.</p>

<p>Your <code>requirements.txt</code> will look something like this:</p>

<pre><code>otherpackage==6.9.3          # via -r requirements.in
somepackage==3.4.2           # via -r requirements.in
transitivedependency==2.7.6  # via somepackage
</code></pre>

<p>It helpfully tells you where everything is from!</p>

<p>Now to actually install these things you <code>python -m pip -r requirements.txt</code>.</p>

<p>Now you’re good to go! Happy hacking!</p>