37

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?

2
  • 2
    The usual solution is to create a virtual environment: docs.python.org/3/tutorial/venv.html Commented Dec 25, 2018 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
    Commented Dec 25, 2018 at 21:23

3 Answers 3

41

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

6
  • 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
    Commented Dec 25, 2018 at 21:22
  • 2
    @henhen if you intend to distribute your package then yes. more info link Commented Dec 25, 2018 at 21:25
  • 2
    How do I give the python interpreter the location of the lib directory when I am running my python file?
    – crxyz
    Commented Jul 1, 2021 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 Commented Jul 2, 2021 at 4:59
  • I've tried using this method, but it doesn't work with all packages. See: stackoverflow.com/questions/69591041/…
    – u84six
    Commented Oct 16, 2021 at 4:41
25

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

2
  • this will install the packages in directory site-packages not in lib as per question. Commented Dec 25, 2018 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. Commented Dec 25, 2018 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.