0

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

bluesummers
  • 11,365
  • 8
  • 72
  • 108
  • 1
    If you're not using csh, take a look at https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then; there might be a more reliable way to get the path to the program. – Jeff Schaller Nov 12 '21 at 13:49
  • Generally speaking, how do you choose between multiple values of `python3.x`? – Jeff Schaller Nov 12 '21 at 13:50
  • @JeffSchaller as I said, safe to assume there will be only one – bluesummers Nov 12 '21 at 14:23
  • At that point, can you just hard-code two `$(dirname)` steps to go up from `bin`, over to `lib`, and into `python3.*`? – Jeff Schaller Nov 12 '21 at 14:25
  • I don't know, I just want some bash code to do exactly that :D You can assume in the solution that there will be only one dir, but you can't assume its exact name... it could be `python3.8` `python3.9` or other versions – bluesummers Nov 12 '21 at 15:08

1 Answers1

2

Is this what you are looking for ?

pypath=/home/asdasd/zxczxc/qweqwe/bin/python
ls -ld ${pypath%bin/python}lib/python*/site-packages/
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • In order to get the exact output I wanted I dropped the `l` flag, leaving only `ls -d ${...` and it worked perfectly – bluesummers Nov 13 '21 at 05:47