pythonpackagevirtualenvrequirements.txtpipfile

How to import (install) dependencies (packages) from a global system environment to a virtual environment


I have installed my needed packages (dependencies) in the global system environment instead of my virtual environment (virtualenv) because i have used the command pip install <package-name> outside of the virtual environment.

So i want to know how can i make a list out of them and install them in any of my virtualenvs?


Solution

  • This is useful in a case that anyhow you have installed some packages (dependencies) in your global system environment instead of your virtualenv, by mistake.

    For example by using the command "pip install", not "pipenv install" (outside of virtual environment).

    So the solution is:

    1. In the global system environment (outside of any virtualenv), create a "requirements.txt" file from all of your installed packages:

      $ pip freeze > requirements.txt
      
    2. Import installed dependencies from the created "requirements.txt" file to the Pipfile by running the below command in the root that the above created file "requirements.txt" exist; but first check:

      a) If Pipfile not exist:

      $ pipenv install
      

      b) If Pipfile do exist (i.e. already created virtualenv):

      $ pipenv install -r requirements.txt
      

    Then your package listing files "Pipfile" & "Pipfile.lock" would be updated and locked.

    But I personally recommend that for avoiding this problem to happen, always use the command $ pipenv install instead of $ pip install.