Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing Python Dependencies locally in project

I am coming from NodeJS and learning Python and was wondering how to properly install the packages in requirements.txt file locally in the project.

For node, this is done by managing and installing the packages in package.json via npm install. However, the convention for Python project seems to be to add packages to a directory called lib. When I do pip install -r requirements.txt I think this does a global install on my computer, similar to nodes npm install -g global install. How can I install the dependencies of my requirements.txt file in a folder called lib?

like image 860
henhen Avatar asked Dec 25 '18 21:12

henhen


3 Answers

use this command
pip install -r requirements.txt -t <path-to-the-lib-directory>

like image 54
sahasrara62 Avatar answered Oct 28 '22 07:10

sahasrara62


If you're looking to install dependencies in special (non-standard) local folder for a specific purpose (e.g. AWS Lambda), see this question: install python package at current directory.

For normal workflows the following is the way to install dependencies locally (instead of globally, equivalent to npm i instead of npm i -g in Node):


The recommended way to do this is by using a virtual environment. You can install virtualenv via pip with

pip install virtualenv

Then create a virtual environment in your project directory:

python3 -m venv env # previously: `virtualenv env`

Which will create a directory called env (you can call it anything you like though) which will mirror your global python installation. Inside env/ there will be a directory called lib which will contain Python and will store your dependencies.

Then activate the environment with:

source env/bin/activate

Then install your dependencies with pip and they will be installed in the virtual environment env/:

pip install -r requirements.txt

Then any time you return to the project, run source env/bin/activate again so that the dependencies can be found.

When you deploy your program, if the deployed environment is a physical server, or a virtual machine, you can follow the same process on the production machine. If the deployment environment is one of a few serverless environments (e.g. GCP App Engine), supplying a requirements.txt file will be sufficient. For some other serverless environments (e.g. AWS Lambda) the dependencies will need to be included in the root directory of the project. In that case, you should use pip install -r requirements.txt -t ./.

like image 29
Henry Woody Avatar answered Oct 28 '22 07:10

Henry Woody


I would suggest getting the Anaconda navigator.

You can download it here: https://www.anaconda.com

Anaconda allows you to create virtual environments through a graphical interface. You can download any pip package that is available through Anaconda.

Then all you have to do after you have created and added onto your environment is to got to your designated python editor (I mainly use Pycharm) and setting the path to the virtual environment’s interpreter when you select or change the interpreter for your project.

Hope this helps.

like image 1
MAS Avatar answered Oct 28 '22 05:10

MAS