DEV Community

Anderson de Alencar
Anderson de Alencar

Posted on ‱ Edited on

1

Trabalhando com Ambientes Virtuais em Python 🐍

É sempre interessante isolar as dependĂȘncias de um projeto e toda linguagem ou framework tem a sua ferramenta: Java e Gradle, Node e npm, etc. No caso do Python, eu uso o Anaconda e Virtualenv e por isso criei uma pequena referĂȘncia para ajudar a trabalhar com os ambientes virtuais.

Anaconda

Anaconda gera um ambiente em um diretĂłrio padrĂŁo da ferramenta, exceto se for especificado o diretĂłrio.

Criando um ambiente virtual

conda create --name <nome>
Enter fullscreen mode Exit fullscreen mode

Mudando de ambiente virtual

conda activate <nome>
Enter fullscreen mode Exit fullscreen mode

Saindo do ambiente virtual

conda deactivate
Enter fullscreen mode Exit fullscreen mode

Listar pacotes instalados

conda list
Enter fullscreen mode Exit fullscreen mode

Listar ambientes instalados

conda env list
Enter fullscreen mode Exit fullscreen mode

Exportar configuraçÔes de um ambiente virtual. Usa-se a flag --from-history para manter a compatibilidade entre plataformas.

conda env export > environment.yml
Enter fullscreen mode Exit fullscreen mode
conda env export --from-history > environment.yml
Enter fullscreen mode Exit fullscreen mode

Removendo um ambiente

conda remove --name myenv --all
Enter fullscreen mode Exit fullscreen mode

Duplicando um ambiente virtual a partir do environment.yml

conda env create -n conda-name -f environment.yml
Enter fullscreen mode Exit fullscreen mode

Atualizar as dependĂȘncias de um ambiente pelo arquivo enviroment.yml

conda env update -f enviroment.yml --prune
Enter fullscreen mode Exit fullscreen mode

Mais informaçÔes Conda environments

Virtualenv

O Virtualenv gera um ambiente virtual na pasta local onde vocĂȘ estiver trabalhando. No Linux, geralmente, coloca-se um ponto no começo do diretĂłrio para ocultĂĄ-lo, mas nĂŁo Ă© obrigatĂłrio.

Criando ambiente virtual (o ponto significa que a pasta do ambiente fica oculta em ambientes Unix, neste caso opcional).

virtualenv .<nome>
Enter fullscreen mode Exit fullscreen mode

Ativando o ambiente

source .<nome>/bin/activate
Enter fullscreen mode Exit fullscreen mode

Para sair

deactivate
Enter fullscreen mode Exit fullscreen mode

Para excluir o ambiente virtual basta deletar a pasta onde foi criado.

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay