1

If I install a python package onto my computer globally, and then add that package's location to my PATH variable, then my command line can find it without me typing out the complete path every time I want to execute.

How does this work inside of a virtual environment? For example I just used pip install ffmpeg inside of my project's activated virtual environment. I can see it inside of my venv/Lib/site-packages folder, but my command line cannot find it. How do I do the virtual environment equivalent of adding ffmpeg's location to my PATH variable?

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
  • 1
    You don't add Python packages to `PATH`; you add the directory that contains the package to `PYTHONPATH`. Typically, you install packages to a directory that is already in your interpreter's default path (especially in a virtual environment), so that you don't need to modify `PYTHONPATH` at all. – chepner Aug 10 '20 at 21:58
  • 3
    It works exactly the same in a virtual environment. If you have activated the virtual environment, then the `pip` you run is the one in your virtual environment, and it is already configured to install the package in a directory your virtual environment's `python` is configured to look for packages. – chepner Aug 10 '20 at 22:00

1 Answers1

1

As chepner mentioned in their comment above, this should already work if you have activated your virtual environment. You can convince yourself or double check which location you are running from by looking at __file__ for the module:

Before activating virtual environment:

>>> import pip
>>> print(pip.__file__)
C:\Users\...\Anaconda3\lib\site-packages\pip\__init__.py

After activating the virtual environment:

>>> import pip
>>> print(pip.__file__)
C:\Users\...\Documents\Research\...\env\lib\site-packages\pip\__init__.py                                                                                                                             

Note that I didn't adjust PATH, and pip is being run from the virtual environment.

Also note that __file__ is not necessarily defined for every module. For a more complete view of options when __file__ doesn't work for a quick check, see this answer.

Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • hmm okay. It's good to know that I can tell from where a file is being run. That will help me troubleshoot in the future. – rocksNwaves Aug 10 '20 at 22:24
  • I tried your little test above, and I get that pip is being loaded from the same exact location either way. – rocksNwaves Aug 10 '20 at 22:37
  • Got it to work. Thank you again. The problem was that I was not prepending my activation command with `.`, as found in this answer here: https://stackoverflow.com/questions/10450992/can-not-activate-a-virtualenv-in-git-bash-mingw32-for-windows – rocksNwaves Aug 10 '20 at 22:47
  • @rocksNwaves, You're probably naively thinking that env vars are global. They are not global on UNIX like systems. The whole reason for using the `.` or `source` shell command is to ensure that any changes made by the script are done in the context of the current shell rather than a subshell; i.e., a child process. – Kurtis Rader Aug 11 '20 at 05:01