(Nov-22-2020, 09:56 AM)Gribouillis Wrote: [ -> ]You can ask python to where it points. For example in my kubuntu 16.04,
λ python3
Python 3.5.2 (default, Oct 7 2020, 17:19:02)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/usr/bin/python3'
By the way, what about installing packages from within Python itself? You would be certain that they are installed for the current interpreter.
import subprocess as sp
import sys
def install_package(name):
sp.call([sys.executable, '-m', 'pip', 'install', '-U', '--user', name])
if __name__ == '__main__':
install_package('requests')
Wow, I think that with your help I have finally figured out what is going on. Perhaps you can check my logic/reasoning.
Outside of a virtual environment:
pip -V
Quote:pip 20.2.3 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6)
$python
Quote:Python 3.7.9 (default, Aug 18 2020, 06:22:45)
[GCC 7.5.0] on linux
>>> import sys
>>> sys.executable
'/usr/bin/python3.7'
So, as shown in the above, packages are put into /usr/local/lib/python3.6 and a python3.7 executable is called. Ok, I can see how that the dis-packages might not be executed by a different executable.
Next case is calling python3
$python3
Quote:Python 3.6.9 (default, Oct 8 2020, 12:12:24)
[GCC 8.4.0] on linux
>>> import sys
>>> sys.executable
'/usr/bin/python3'
lrwxrwxrwx 1 root root 9 Mar 3 2019 python3 -> python3.6
A python3.6 executable is able to run the packages in /usr/local/lib/python3.6. That would make sense.
So now, lets go into the python virtual environment that I set up with anaconda.
conda activate pytorchbook
(pytorchbook)$pip -V
Quote:pip 20.2.4 from ~/anaconda3/envs/pytorchbook/lib/python3.7/site-packages/pip (python 3.7
Packages in this environment are in a different place that in the cases outside of the environment.
Entering python:
$python
Quote:Python 3.7.9 (default, Aug 18 2020, 06:22:45)
[GCC 7.5.0] on linux
>>> import sys
>>> sys.executable
'/usr/bin/python3.7'
In this case, the same executable is called inside and outside of the virtual environment. And, I am
NOT able to import modules from ~/anaconda3/envs/pytorchbook/lib/python3.7.
Last case, entering python3 from inside the virtual environment:
Quote:Python 3.7.9 (default, Aug 31 2020, 12:42:55)
[GCC 7.3.0] :: Anaconda, Inc. on linux
>>> import sys
>>> sys.executable
'~/anaconda3/envs/pytorchbook/bin/python3'
So, the python3 executable from within the pytorch environment can be used to import modules from within ~/anaconda3/envs/pytorchbook/lib/python3.7
Not sure of why two python executables of python 3.7.9 don't behave the same other than the fact that they were compiled under different versions of GCC as shown.
Does the above make sense?
Thank you for all of your help.
Tom