Skip to content

Instantly share code, notes, and snippets.

@andrzejsliwa
Last active July 19, 2024 11:09
Show Gist options
  • Save andrzejsliwa/51970076661ec8dd1b730232538801a2 to your computer and use it in GitHub Desktop.
Save andrzejsliwa/51970076661ec8dd1b730232538801a2 to your computer and use it in GitHub Desktop.
Simple tutorial for using pyenv and pipenv all together (seamlessly)
#!/usr/bin/env -S pipenv run python
import sys
import dateparser
def main(argv):
print(argv)
print("ello")
print(dateparser.parse('12/12/12'))
if __name__ == '__main__':
main(sys.argv)

Simple tutorial for using pyenv and pipenv all together (seamlessly)

Setup

Install pyenv & pipenv:

brew install pyenv pipenv

Add to your .bashrc:

eval "$(pyenv init -)"
eval "$(_PIPENV_COMPLETE=bash_source pipenv)"

Create new project folder:

mkdir learning_python 

Go to new project folder (please notice that I have globally installed python 3.12.4, shown by starship prompt):

cd learning_python
~/learning_python via 🐍 v3.12.4
>

Ensure that you don't have any previously set up versions (I had because I played with it a lot):

~/learning_python via 🐍 v3.12.4
  pipenv --rm
Removing virtualenv (/home/andrzejsliwa/.local/share/virtualenvs/learning_python-vljVMfmz)...

Now setup project for specific version (pipenv will execute/use for you pyenv underhood):

~/learning_python via 🐍 v3.12.4
  pipenv --python 3.9
Warning: Python 3.9 was not found on your system...
Would you like us to install CPython 3.9.19 with Pyenv? [Y/n]: y
Installing CPython 3.9.19 with /home/linuxbrew/.linuxbrew/bin/pyenv (this may take a few minutes)...
βœ” Success!
β Ό Installing python...python-build: use readline from homebrew
python-build: use ncurses from homebrew
python-build: use zlib from homebrew

Creating a virtualenv for this project...
Pipfile: /home/andrzejsliwa/learning_python/Pipfile
Using /home/andrzejsliwa/.pyenv/versions/3.9.19/bin/python3 (3.9.19) to create virtualenv...
β Έ Creating virtual environment...created virtual environment CPython3.9.19.final.0-64 in 277ms
creator CPython3Posix(dest=/home/andrzejsliwa/.local/share/virtualenvs/learning_python-vljVMfmz, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/andrzejsliwa/.local/share/virtualenv)
    added seed packages: pip==24.0, setuptools==69.5.1, wheel==0.43.0
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

βœ” Successfully created virtual environment!
Virtualenv location: /home/andrzejsliwa/.local/share/virtualenvs/learning_python-vljVMfmz
Creating a Pipfile for this project...

Now lets check our Pipfile:

~/learning_python via 🐍 v3.12.4 took 2m15s
  cat Pipfile
────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    β”‚ File: Pipfile
────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1   β”‚ [[source]]
2   β”‚ url = "https://pypi.org/simple"
3   β”‚ verify_ssl = true
4   β”‚ name = "pypi"
5   β”‚
6   β”‚ [packages]
7   β”‚
8   β”‚ [dev-packages]
9   β”‚
10  β”‚ [requires]
11  β”‚ python_version = "3.9"
12  β”‚ python_full_version = "3.9.19"
────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Now lets run shell for specific version (defined in Pipefile, please notice version change in prompt):

~/learning_python via 🐍 v3.12.4
  pipenv shell
Launching subshell in virtual environment...
. /home/andrzejsliwa/.local/share/virtualenvs/learning_python-vljVMfmz/bin/activate
~/learning_python via 🐍 v3.9.19 (learning_python)


Now you are able to go back (by exiting shell with specific version):

~/learning_python via 🐍 v3.9.19 (learning_python)
  exit
~/learning_python via 🐍 v3.12.4 took 1m59s

Now I changed version of python in Pipfile to 3.10.0 and executed shell again (please notice downloading new version and switching in prompt):

  cat Pipfile
────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    β”‚ File: Pipfile
────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1   β”‚ [[source]]
2   β”‚ url = "https://pypi.org/simple"
3   β”‚ verify_ssl = true
4   β”‚ name = "pypi"
5   β”‚
6   β”‚ [packages]
7   β”‚
8   β”‚ [dev-packages]
9   β”‚
10  β”‚ [requires]
11  β”‚ python_version = "3.10"
────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

~/learning_python via 🐍 v3.12.4
  pipenv shell
Creating a virtualenv for this project...
Pipfile: /home/andrzejsliwa/learning_python/Pipfile
Using /home/andrzejsliwa/.pyenv/versions/3.10.0/bin/python3 (3.10.0) to create virtualenv...
β Έ Creating virtual environment...created virtual environment CPython3.10.0.final.0-64 in 280ms
creator CPython3Posix(dest=/home/andrzejsliwa/.local/share/virtualenvs/learning_python-vljVMfmz, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/andrzejsliwa/.local/share/virtualenv)
    added seed packages: pip==24.0, setuptools==69.5.1, wheel==0.43.0
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

βœ” Successfully created virtual environment!
Virtualenv location: /home/andrzejsliwa/.local/share/virtualenvs/learning_python-vljVMfmz
Launching subshell in virtual environment...
. /home/andrzejsliwa/.local/share/virtualenvs/learning_python-vljVMfmz/bin/activate
~/learning_python via 🐍 v3.10.0 (learning_python)

Regular usage

Installing few development deps (not required by production code):

pipenv install debugpy --dev
Installing debugpy...
Resolving debugpy...
Added debugpy to Pipfile's [dev-packages] ...
βœ” Installation Succeeded
Pipfile.lock not found, creating...
Locking [packages] dependencies...
Locking [dev-packages] dependencies...
Building requirements...
Resolving dependencies...
βœ” Success!
Updated Pipfile.lock (6673b63aebe4b660d4711e3e1e3300459b8598c87edc3815cb1fff7a97d56c83)!
Installing dependencies from Pipfile.lock (d56c83)...
Installing dependencies from Pipfile.lock (d56c83)...

~/learning_python via 🐍 v3.10.0 (learning_python) took 6s
  pipenv install pytest --dev
Installing pytest...
Resolving pytest...
Added pytest to Pipfile's [dev-packages] ...
βœ” Installation Succeeded
Pipfile.lock (d56c83) out of date: run `pipfile lock` to update to (f1a44f)...
Running $ pipenv lock then $ pipenv sync.
Locking [packages] dependencies...
Locking [dev-packages] dependencies...
Building requirements...
Resolving dependencies...
βœ” Success!
Updated Pipfile.lock (38b9c301f5151834f151f66674f8f71193df106ab354eeaaaa9b4a16a5f1a44f)!
Installing dependencies from Pipfile.lock (f1a44f)...
All dependencies are now up-to-date!
Installing dependencies from Pipfile.lock (f1a44f)...
Installing dependencies from Pipfile.lock (f1a44f)...
~/learning_python via 🐍 v3.10.0 (learning_python) took 7s


Now lets add regular deps (not dev):

~/learning_python via 🐍 v3.10.0 (learning_python)
  pipenv install dateparser
Installing dateparser...
Resolving dateparser...
Added dateparser to Pipfile's [packages] ...
βœ” Installation Succeeded
Pipfile.lock (f1a44f) out of date: run `pipfile lock` to update to (5d79fe)...
Running $ pipenv lock then $ pipenv sync.
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
βœ” Success!
Locking [dev-packages] dependencies...
Building requirements...
Resolving dependencies...
βœ” Success!
Updated Pipfile.lock (cae7c3a1e0d632468f093a017aa5285ccdd78dfed3c1c14a81e9c1b4875d79fe)!
Installing dependencies from Pipfile.lock (5d79fe)...
All dependencies are now up-to-date!
Installing dependencies from Pipfile.lock (5d79fe)...
~/learning_python via 🐍 v3.10.0 (learning_python) took 17s

Please notice that all deps/packages are defined in Pipfile.lock, even sub deps

~/learning_python via 🐍 v3.10.0 (learning_python)
>  cat Pipfile.lock

When you would like to install all deps (including dev) from project Project you can call:

~/learning_python via 🐍 v3.10.0
>  pipenv install --dev

Interesting fact if you will create .env file, pipenv shell will pick it up:

~/learning_python via 🐍 v3.12.4 took 8s
  pipenv shell
Loading .env environment variables...
Loading .env environment variables...
Launching subshell in virtual environment...
. /home/andrzejsliwa/.local/share/virtualenvs/learning_python-vljVMfmz/bin/activate
~/learning_python via 🐍 v3.12.4
   . /home/andrzejsliwa/.local/share/virtualenvs/learning_python-vljVMfmz/bin/activate
~/learning_python via 🐍 v3.10.0 (learning_python)

Creating executable scripts

If you would like to create executable script you can make it with adding proper shebang which is using pipenv

Content of file 'ex5':

#!/usr/bin/env -S pipenv run python
import sys
import dateparser

def main(argv):
    print(argv)
    print("ello")
    print(dateparser.parse('12/12/12'))

if __name__ == '__main__':
    main(sys.argv)

You can then add executable rights to it

~/learning_python via 🐍 v3.12.4
  chmod +x ex5

And execute it (please notice that usage of dateparser from our previosly defined deps)

~/learning_python via 🐍 v3.12.4
  ./ex5
Loading .env environment variables...
['./ex5']
ello
2012-12-12 00:00:0

Migrating from requirements.txt

If you project containing old requirements.txt you can easly migrate it (please notice recommendation at the end):

~/learning_python via 🐍 v3.12.4
  pipenv --python 3.9
Loading .env environment variables...
requirements.txt found in /home/andrzejsliwa/learning_python instead of Pipfile! Converting...
βœ” Success!
Warning: Your Pipfile now contains pinned versions, if your requirements.txt did.
We recommend updating your Pipfile to specify the "*" version, instead.

Recommendation suggesting specify the * version as deps are locked anyway in Pipfile.lock

If you wish you can also generate requirements out of you Pipfiles:

~/learning_python via 🐍 v3.12.4
  pipenv requirements > requirements.txt

~/learning_python via 🐍 v3.12.4
  pipenv requirements --dev > dev-requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment