Skip to content

Commit 9b168f3

Browse files
committed
Adds pipenv to the chapter
1 parent b77984d commit 9b168f3

File tree

1 file changed

+166
-6
lines changed

1 file changed

+166
-6
lines changed

docs/virtualenv.rst

Lines changed: 166 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
Virtualenv
55
==========
66

7-
Virtual Python Environment or venv is a Python environment which will help you to install different versions of Python modules in a local directory using which you can develop and test your code without requiring to install everything systemwide.
7+
Virtual Python Environment or venv is a Python environment which will help you
8+
to install different versions of Python modules in a local directory using which
9+
you can develop and test your code without requiring to install everything
10+
systemwide.
811

912
Installation
1013
============
1114

12-
In Python3 we can use the *venv* module to create virtual environments.
15+
In Python3 we can use the **venv** module to create virtual environments.
1316

1417
Usage
1518
=====
1619

17-
We will create a directory call *virtual* inside which we will have two different virtual environment.
20+
We will create a directory call *virtual* inside which we will have two
21+
different virtual environment.
1822

1923
The following commands will create an env called virt1.
2024

@@ -31,7 +35,9 @@ Now we can activate the virt1 environment.
3135
$ source virt1/bin/activate
3236
(virt1)[user@host]$
3337

34-
The first part of the prompt is now the name of the virtual environment, it will help you identify which environment you are in when you have multiple environments.
38+
The first part of the prompt is now the name of the virtual environment, it
39+
will help you identify which environment you are in when you have multiple
40+
environments.
3541

3642
To deactivate the environment use *deactivate* command.
3743

@@ -52,7 +58,8 @@ So, now we will install a Python module called redis.
5258
Successfully installed redis-2.10.5
5359

5460

55-
Now we will create another virtual environment *virt2* where we will install the same redis module but an old 2.4 version of it.
61+
Now we will create another virtual environment *virt2* where we will
62+
install the same redis module but an old 2.4 version of it.
5663

5764
::
5865

@@ -68,8 +75,161 @@ Now we will create another virtual environment *virt2* where we will install the
6875
Successfully installed redis
6976
Cleaning up...
7077

71-
This way you can have many different environments for all of your development needs.
78+
This way you can have many different environments for all of your development
79+
needs.
7280

7381
.. note:: Always remember to create virtualenvs while developing new applications. This will help you keep the system modules clean.
7482

7583

84+
Pipenv
85+
=======
86+
87+
`Pipenv <https://docs.pipenv.org/>`_ is a tool created by `Kenneth Reitz
88+
<https://www.kennethreitz.org/>`_ which helps to create, manage the
89+
virtualenvs for your projects. It also helps to install/uninstall/update the
90+
dependencies of your project.
91+
92+
93+
Installing pipenv
94+
------------------
95+
96+
We can install pipenv by the following command.
97+
98+
::
99+
100+
$ python3 -m pip install --user pipenv
101+
102+
103+
Using pipenv
104+
-------------
105+
106+
You can go to your project directory, and then use the command **pipenv
107+
install** to create a new virtualenv for you. You can also pass any module
108+
name which *pipenv* will install on the environment.
109+
110+
::
111+
112+
$ mkdir myproject
113+
$ cd myproject
114+
$ pipenv install requests
115+
Creating a virtualenv for this project…
116+
Using /usr/bin/python3 (3.6.5) to create virtualenv…
117+
⠋Already using interpreter /usr/bin/python3
118+
Using base prefix '/usr'
119+
New python executable in /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l/bin/python3
120+
Also creating executable in /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l/bin/python
121+
Installing setuptools, pip, wheel...done.
122+
123+
Virtualenv location: /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l
124+
Creating a Pipfile for this project…
125+
Installing requests…
126+
Collecting requests
127+
Downloading https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)
128+
Collecting chardet<3.1.0,>=3.0.2 (from requests)
129+
Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
130+
Collecting idna<2.7,>=2.5 (from requests)
131+
Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)
132+
Collecting certifi>=2017.4.17 (from requests)
133+
Using cached https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl
134+
Collecting urllib3<1.23,>=1.21.1 (from requests)
135+
Downloading https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)
136+
Installing collected packages: chardet, idna, certifi, urllib3, requests
137+
Successfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
138+
139+
Adding requests to Pipfile's [packages]…
140+
Pipfile.lock not found, creating…
141+
Locking [dev-packages] dependencies…
142+
Locking [packages] dependencies…
143+
Updated Pipfile.lock (b14837)!
144+
Installing dependencies from Pipfile.lock (b14837)…
145+
🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 — 00:00:02
146+
To activate this project's virtualenv, run the following:
147+
$ pipenv shell
148+
149+
The above command will create a new virtualenv and then also install
150+
*requests* module in the environment. You can then use **pipenv shell**
151+
command to activate that environment. For our example, we will use
152+
the following Python code in a file named *main.py*.
153+
154+
::
155+
156+
import requests
157+
response = requests.get('https://httpbin.org/ip')
158+
print('Your IP is {0}'.format(response.json()['origin']))
159+
160+
161+
::
162+
163+
$ pipenv shell
164+
$ $ python main.py
165+
Your IP is 192.168.1.2
166+
167+
Exiting from the virtualenv
168+
----------------------------
169+
170+
You can exit from the virtualenv using **exit** command, or by pressing *Ctrl+d*.
171+
172+
173+
Pipfile and Pipfile.lock
174+
=========================
175+
176+
If you notice your project directory after you have used **pipenv**, you will
177+
find two new files inside, *Pipfile* and *Pipfile.lock*. These files have been
178+
created by the **pipenv** command. You should checkin these two files into
179+
your version control system (say: git), so that others can create the exact
180+
same environment of yours.
181+
182+
Pipfile
183+
--------
184+
185+
The following is the content of our *Pipfile*. It is using the `TOML
186+
<https://en.wikipedia.org/wiki/TOML>`_ file format.
187+
188+
::
189+
190+
[[source]]
191+
verify_ssl = true
192+
name = "pypi"
193+
url = "https://pypi.python.org/simple"
194+
195+
[dev-packages]
196+
197+
[requires]
198+
python_version = "3.6.5"
199+
200+
[packages]
201+
requests = "*"
202+
203+
On the top it tells which source to use to get the packages. It also mentions
204+
the Python version required. The packages section tells us what all Python
205+
packages we need. The string `"*"` means install the latest version available
206+
on the package index. The exact version details of the packages are stored in
207+
the *Pipfile.lock* file, it is in machine readable `JSON
208+
<https://en.wikipedia.org/wiki/JSON>`_ format.
209+
210+
Remember to install any dependency for your project using **pipenv** comamnd,
211+
that will automatically update your *Pipfile* and *Pipfile.lock* file. If you
212+
have any dependency which is only required for the development, you can
213+
install them marked as *dev-packages*. In the following example I am installing
214+
*flake8* as development dependency.
215+
216+
::
217+
218+
$ pipenv install --dev flake8
219+
$ cat Pipfile
220+
[[source]]
221+
verify_ssl = true
222+
name = "pypi"
223+
url = "https://pypi.python.org/simple"
224+
225+
[dev-packages]
226+
"flake8" = "*"
227+
228+
[requires]
229+
python_version = "3.6.5"
230+
231+
[packages]
232+
requests = "*"
233+
234+
You can watch `this talk <https://www.youtube.com/watch?v=GBQAKldqgZs>`_ by
235+
Kenneth from PyCon 2018 to know more about *Pipenv*.

0 commit comments

Comments
 (0)