Issues with importing modules on mac (Big Sur), and sys.executable and location of package not matching up?

Viewed 382

I have an issue in which I'll install a package (like tensorflow or dpu_utils), and it will end up in a different location than sys.executable. For instance, when I run pip show dpu_utils on my terminal, I get the following information:

Name: dpu-utils
Version: 0.2.20
Summary: Python utilities used by Deep Procedural Intelligence
Home-page: https://github.com/microsoft/dpu-utils
Author: Deep Procedural Intelligence
Author-email: None
License: MIT
Location: /usr/local/lib/python3.9/site-packages
Requires: docopt, tqdm, sentencepiece, azure-storage, numpy, SetSimilaritySearch
Required-by: 

When I run python, import dpu_utils, import sys, and then sys.executable, I get the following:

python
Python 3.7.3 (default, Dec 13 2019, 19:58:14) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dpu_utils
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dpu_utils'
>>> import sys
>>> sys.executable
'/Library/Developer/CommandLineTools/usr/bin/python3'
>>> exit()

As you can see, the issue arises because my sys.executable does not match up with the location of where the package is actually being installed. Is there a way to set the sys.executable such that it matches to /usr/local/lib/python3.9/site-packages? I am very confused on how to do this since updating to Big Sur on my Mac. I tried to edit the PATH variable in .zshrc, but no such luck. If anyone could give me a step by step of how to edit my environment variables so my programs find the package materials where they are actually getting installed, it would be incredibly helpful. Thank you.

1 Answers

To install package to a specific path, use pip with --target

But I recommend using it in a very specific case. Because if everything is configured correctly, there is no need to give packages specific paths.

I had a problem like this. It seems to me that you have a problem in different installed versions of python 3.7 and 3.9. This means that pip will install packages in different folders, as well as refer to them in different virtual environments (if they were created). Installed in one place, run from another. Well, or the environment thinks that it is necessary to contact it there, since several different versions of python are probably installed. Or, the python was updated incorrectly.

What can be done to put things in order:

  • Optional - find and delete all mentions of python and packages manually.
  • Update (install) python, update pip. Create a new virtual environment with the required python version.
  • Сheck python and pip versions in active environment.
  • Install all packages only from this environment and run scripts only from it.
Related