How to install modules from a Python project with Flask?

Asked

Viewed 52 times

0

I’m used to working with Javascript, and it’s common to download a Github project and run the command yarn to download all the modules that that project uses so that I can run the project.

However, I decided to work with Flask and I don’t understand how the installation of the modules works. Flask also works with modules installed as the flask and the flask-restplus, and when I send my Flask code to Github, none of the downloaded modules are sent. So far so good, but when I download the repository elsewhere, I don’t know how to reinstall the modules used in that project.

Have any way to reinstall these modules or I would have to reinstall one by one and create a new local venv?

  • Please clarify your problem or provide additional details in order to highlight exactly what you need. The way it’s written these days it’s hard to tell exactly what you’re asking.

1 answer

-1


You must install the dependencies using Pip (python index Packages).

In general (this is a convention) the dependencies are listed in a file, with the name:

requirements.txt

Which also by convention is located at raiz do projeto.

This way when downloading a project to install the dependencies just run the command:

pip install requirements.txt

These conventions are not part of any specific EP, but are documented in the python backpacker guide

Running the above command, the dependencies will be installed, so you can start the application.

Good practices related to the creation of a new environment

When cloning the project the ideal is to use the virtualenv to create a new clean environment (without external dependencies) and from this environment proceed to the installation of dependencies.

Create the virtualenv

To create the virtual environment I use the virtualenv program. Running the following command:

virtualenv venv -p $(which python3)

This command will create a directory name venv (can choose the name) in the folder where it was executed, in this case the directory where I am (cwd) using python3 binaries.

Note: from the version of python3.3, a mechanism for creating virtual environments incorporated into Python3 was included. If this is the case you don’t need to have virtualenv installed and to create the virtual environment, just run the comando:

python3 -m venv </caminho/para/novo/ambiente/virtual>

More information can be found on PEP 405

Activating the virtualenv

To activate the virtual environment, using the terminal (bash, sh), we must execute the command:

source venv/bin/activate #para linux
#para Windows é necessário executar scripts/activate.bat

After activating the virtual environment we can proceed to the installation of the dependencies. In Linux-based environments an indicator that the virtual environment is active is that in the shell runline the name of the environment appears in parentheses.

As in the example:

(venv) user@ubuntu:~$ #<-- virtualenv ativo
user@ubuntu:~$ #<-- virtualenv não está ativo, isso é o shell padrão.

Setting up the offices

After activating virtualenv now just install the dependencies, with the command:

pip install -r requirements.txt

Summary

  1. clone the project:

    git clone <endereço do projeto em algum repositório>

  2. Enter the cloned project folder

  3. Creating the virtual environment:

    virtualenv venv -p $(which python3)

  4. activate the virtual environment:

    source venv bin/activate

  5. install the dependencies:

    pip install -r requirements.txt

  • 1

    Thank you very much, I killed myself to find this on the internet, but without a reference was difficult, valeuzão!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.