30

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?

henhen
  • 1,038
  • 3
  • 18
  • 36
  • 2
    The usual solution is to create a virtual environment: https://docs.python.org/3/tutorial/venv.html – UnholySheep Dec 25 '18 at 21:17
  • 1
    Yes, I created a virtual env, but I was told that this is only for local testing, and that I should still use the `lib` directory. Although it seems like doing it this way is also for local testing as I assume the `lib` directory with all my dependencies should be included in the `.gitignore` file. – henhen Dec 25 '18 at 21:23

3 Answers3

32

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

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • 4
    Thanks! Not sure why this isn't well documented. I coulnd't find anything on how to do this. Should my folder where I install my packages be included in the `.gitignore` file? – henhen Dec 25 '18 at 21:22
  • 2
    @henhen if you intend to distribute your package then yes. more info [link](https://git-scm.com/docs/gitignore) – sahasrara62 Dec 25 '18 at 21:25
  • 1
    How do I give the python interpreter the location of the lib directory when I am running my python file? – CrazyVideoGamer Jul 01 '21 at 23:09
  • @CrazyVideoGamer create a python package of that lib and then install it in your python environment, you can use it that way or [this may help](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – sahasrara62 Jul 02 '21 at 04:59
  • I've tried using this method, but it doesn't work with all packages. See: https://stackoverflow.com/questions/69591041/installing-python-packages-locally-doesnt-always-work/69591768#69591768 – u84six Oct 16 '21 at 04:41
  • @u84six i hope your problem is solved with the Blckknght answer – sahasrara62 Oct 16 '21 at 05:51
17

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 ./.

Henry Woody
  • 14,024
  • 7
  • 39
  • 56
  • this will install the packages in directory `site-packages` not in `lib` as per question. – sahasrara62 Dec 25 '18 at 21:31
  • 1
    @prashantrana packages will be installed in `env/lib/pythonx.x/site-packages` so not directly in `lib` but still within `lib`. Also OP noted that this was common practice to do, so It's no so much that OP wants dependencies in `lib` but rather to follow best practices. And using a virtual environment is a best practice. – Henry Woody Dec 25 '18 at 21:33
0

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.

MAS
  • 84
  • 8