pythonsys.path

Appending module sys.path.append


I am running a python script on a local server (my OS is Ubuntu 20.04). The script is located here:

/var/www/html/myfoo.py

I need to use numpy module. The module is located here:

/home/namae/.local/lib/python3.8/site-packages

I am appending the module:

try:
    import sys
    sys.path.append('/home/namae/.local/lib/python3.8/site-packages')
    import numpy
except Exception as e:    
    print(e)

I am getting an error: No module named 'numpy'

I don't understand what I'm doing wrong

Update:

  1. Unfortunately, I ran into difficulties at the stage of installing conda and following the suggested solution.
  2. sys.executable is /usr/bin/python3
  3. ls -lsa $(which python) gives me in the working dir:
4 -rw-r--r--  1 root root  123 Apr 26 00:53 myfoo.py
  1. The following helped me:
sudo cp /home/namae/.local/lib/python3.8/site-packages/numpy* /var/www/html/

Solution

  • Use conda.

    Create an environment.yml:

    name: myfoo
    
    channels:
    - conda-forge
    
    dependencies:
    - numpy >= 1.23.5
    

    Activate the environment. Observe that

    $ python -c 'import numpy'
    

    silently succeeds.