Best practice to use virtualenv in a docker container

https://hynek.me/articles/virtualenv-lives/

virtualenv Lives!

Setting up Python to the point to be able install packages from PyPI can be annoying and time-intensive. Even worse are OS-provided installations that start throwing cryptic error messages. Especially desktops are prone to that but it’s possible to break the whole toolchain of a server by installing some shiny package you heard about on reddit.

Your desktop system is unlikely to be a throwaway virtual machine or container. Which makes it a highly mutable system with difficult rollbacks and a lot of pain if stuff breaks. So until we all run NixOS on our desktops:

Don’t pip-install anything into its global site-packages beyond virtualenv.

Does that sound extreme to you? Only if you haven’t found the right tools to make it effortless.

virtualenv in 2014?

virtualenv has been around for a while and was a somewhat accepted standard for installing Python software. Sadly, there are many missionaries running around nowadays, boldly proclaiming the end of virtualenv. Mostly because of containers in general and usually because of Docker in particular.

I find that unfortunate and shortsighted. Frankly, they fail to see the whole picture: virtualenv’s job isn’t just to separate your projects from each other. Its job is also to separate you from the operating system’s Python installation and the installed packages you probably have no idea about.

Let’s use the widely celebrated virtualenv-killer Docker to see why that’s a good idea. For that we look at the pre-installed packages you get after installing only python-pip into a trusty container1:

argparse (1.2.1)
chardet (2.0.1)
colorama (0.2.5)
html5lib (0.999)
pip (1.5.4)
requests (2.2.1)
setuptools (3.3)
six (1.5.2)
urllib3 (1.7.1)
wsgiref (0.1.2)

Surprised2? What happens if you install a newer requests, html5lib, or colorama over it? I’ll tell you what: stuff starts breaking.

Such things can happen any time and make your system fragile. Full-featured Ubuntu servers carry even more baggage of course. Whenever you install a system tool written in Python you can expect some kind of breakage. Whenever debian packagers decide that they don’t like something about how pip works and patch around it you’re involuntarily part of the “will it explode?” lottery.

OS X is in no way different; it comes with several dozens of Python packages.

And on the desktop – no matter what platform! – the situation is even worse. I dare to say that the average site-packages is a mess and most users have no idea why a certain package is installed. The step to break the whole installation is a very short one as many tutorial mentors will confirm.


The operating system Python’s site-packages3 belongs to the operating system.

I’ve been saying for a while now that I would prefer if OS vendors would create a virtualenv for their stuff somewhere else and let the users have the system site-packages. But that’s not happening. And no one can guarantee that some system tool you don’t even know about won’t ever install a version of a library that’s incompatible with your project’s requirements. Is it worth it to take that chance?

Only put software into site-packages that is explicitly written for that version of the OS. In other words: system tools that only use Python packages provided by the OS. Keep everything else in virtualenv.

Stop discussing virtualenv vs. system isolation as if they were mutually exclusive. You should use both at once, neither replaces the other:

  1. Do isolate your application server’s OS from its host using Docker/lxc/jails/zones/kvm/VMware/… to one container/vm per application.
  2. But inside of them also do isolate your Python environment using virtualenv from unexpected surprises in the system site-packages.

Less Typing, More Happiness

On your desktop you’ll want a bit more convenience than pure virtualenvs. So I urge you take five minutes to install and understand virtualenvwrapper, virtualenvwrapper-win, or virtualfish – depending on the shell and operating system you use. They take all the hassle from managing per-project virtualenvs.

Servers are a bit different because you’re unlikely to have more than one application (i.e. virtualenv) per user or even per server. You can have a look at my current approach to packaging and installing virtualenvs of server applications if you’re interested.

What About Python CLI Tools?

There’s one question that arises from this: what about all the amazing Python-based tools we love? How do we install tox, mynt, httpie, Pygments, and so forth? Arguably, they made the biggest mess in my system installations in the past.

Should you create virtualenvs for them all and link the executable scripts into some directory within you PATH?

‘Yes’ and ‘no’. ‘Yes’, that’s the correct approach (and it has been taken before). ‘No’, you shouldn’t do it yourself. There’s a helpful (alas POSIX–only) tool for just that: pipsi.

After installation (do not try to install it using pip, please read the installation instructions) you can install Python CLI tools by calling e.g.

$ pipsi install Pygments

pipsi then will create a new virtualenv in ~/.local/venvs and install the package into it. Finally it links the scripts into ~/.local/bin which you can add to your PATH.

Footnotes

  1. Built from an official trusty image as of 2014-09-14. ??
  2. The reason for the presence of so many high profile packages is that debian decided to recursively unwrap all dependencies that are bundled within pip (including requests). They’re begging for version conflicts. ??
  3. It doesn’t matter whether it’s one physical directory or many merged. Python doesn’t allow for multiple versions of one package in any case. ??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末猾漫,一起剝皮案震驚了整個(gè)濱河市矿辽,隨后出現(xiàn)的幾起案子讯柔,更是在濱河造成了極大的恐慌尉姨,老刑警劉巖鹃觉,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件姚淆,死亡現(xiàn)場(chǎng)離奇詭異罐孝,居然都是意外死亡县匠,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門笙蒙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)抵屿,“玉大人,你說(shuō)我怎么就攤上這事捅位。” “怎么了搂抒?”我有些...
    開封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵艇搀,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我求晶,道長(zhǎng)焰雕,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任芳杏,我火速辦了婚禮矩屁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘爵赵。我一直安慰自己吝秕,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開白布空幻。 她就那樣靜靜地躺著烁峭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上约郁,一...
    開封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天缩挑,我揣著相機(jī)與錄音,去河邊找鬼鬓梅。 笑死供置,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的绽快。 我是一名探鬼主播士袄,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼谎僻!你這毒婦竟也來(lái)了娄柳?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤艘绍,失蹤者是張志新(化名)和其女友劉穎赤拒,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體诱鞠,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡挎挖,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了航夺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蕉朵。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖阳掐,靈堂內(nèi)的尸體忽然破棺而出始衅,到底是詐尸還是另有隱情,我是刑警寧澤缭保,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布汛闸,位于F島的核電站,受9級(jí)特大地震影響艺骂,放射性物質(zhì)發(fā)生泄漏诸老。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一钳恕、第九天 我趴在偏房一處隱蔽的房頂上張望别伏。 院中可真熱鬧,春花似錦忧额、人聲如沸厘肮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)轴脐。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間大咱,已是汗流浹背恬涧。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留碴巾,地道東北人溯捆。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像厦瓢,于是被迫代替她去往敵國(guó)和親提揍。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 2,696評(píng)論 0 3
  • 體驗(yàn): 今天《文化系統(tǒng)》學(xué)習(xí)課堂上做了一個(gè)活動(dòng)煮仇,就是分二組劳跃,每個(gè)組成員圍成一個(gè)圓,雙手搭在伙伴的肩膀上做深...
    彭紅霞_a827閱讀 157評(píng)論 0 0
  • //正則:就是以個(gè)規(guī)則 用來(lái)處理字符串的一個(gè)規(guī)則 var reg =/\d/; reg.exec("1"); //...
    Kyle_kk閱讀 702評(píng)論 0 1
  • 時(shí)間不肯回頭浙垫, 只因希望總在前方刨仑。
    袁益君閱讀 167評(píng)論 1 2
  • 背景: 為了解決遠(yuǎn)隔兩地的研究者共享知識(shí)的設(shè)想。通過(guò)多文件之間相互關(guān)聯(lián)形成的超文本夹姥,連成可相互參閱的WWW(萬(wàn)維網(wǎng)...
    不懂和尚閱讀 324評(píng)論 0 0