Home > Software engineering >  Bash - command/script for getting Python's site-packages path from interpreter path
Bash - command/script for getting Python's site-packages path from interpreter path

Time:11-14

While inside a Python virtual environment I can execute which python to get the path of the interpreter.

Once I get the path of the interpreter, I can find the site-packages library following some simple steps

  1. Get the interpreter path using which python, this will lead to something like /my/interp/full/path/bin/python
  2. cd to one level above, so we'll be at /my/interp/full/path
  3. cd to lib, now wer'e at /my/interp/full/path/lib
  4. Assume there is only one directory in the current directory, which will be called python3.x, the value of x depends on the version and cd into it, now we're at /my/interp/full/path/lib/python3.8
  5. site-packages will be inside this directory

So what I'm trying to achieve is some kind of shell command/script that will use which python and will output the full path to the site-packages directory

Example of input->output: If the output of which python is /home/asdasd/zxczxc/qweqwe/bin/python the output should be /home/asdasd/zxczxc/qweqwe/lib/python3.8/site-packages/

I'm looking for a bash solution, not Python code

CodePudding user response:

Is this what you are looking for ?

pypath=/home/asdasd/zxczxc/qweqwe/bin/python
ls -ld ${pypath%bin/python}lib/python*/site-packages/
  • Related