0

I have a dockerfile as below:

FROM python:3.7.5-alpine3.10

RUN apk update

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN apk add --no-cache cython3

CMD [ "sh", "ls"]

When I got into the container with docker run -it --rm mycontainer /bin/sh cython appears not to be installed. What am I missing?

/usr/src/app # which python
/usr/local/bin/python
/usr/src/app # python
Python 3.7.5 (default, Oct 21 2019, 20:13:45) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cython
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cython'
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
user2242044
  • 8,803
  • 25
  • 97
  • 164

1 Answers1

3

Alpine installed python pacakges using this path /usr/lib/python3.7/site-packages, just run the command inside the container and you will see the package is installed. All you need to add this path to the python search path.

RUN apk add --no-cache cython3
ENV PYTHONPATH /usr/lib/python3.7/site-packages

PYTHONPATH

Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.

In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles containing pure Python modules (in either source or compiled form). Extension modules cannot be imported from zipfiles.

The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.

An additional directory will be inserted in the search path in front of PYTHONPATH as described above under Interface options. The search path can be manipulated from within a Python program as the variable sys.path.

python envvar PYTHONPATH

update:

To work with pip installation you need to use -m.

When called with -m module-name, the given module is located on the Python module path and executed as a script.

python3-cmdline

you can test

RUN apk add --no-cache cython3
ENV PYTHONPATH /usr/lib/python3.7/site-packages
RUN python -m pip install requests
RUN python -m pip list
#import test
RUN python -c "import requests"
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • This does work and I see that cython is installed and I can import it in python, but if I do pip install cython it still installs it. How can I get it to recognize it's already installed? – user2242044 Nov 05 '19 at 12:18
  • are you sure that you still can use pip when you set the obove pythonpath ? I think pip will not be usable, only by calling `python -m pip` – LinPy Nov 05 '19 at 12:44
  • @LinPy only if I exec into the container it works. `which pip` returns `/usr/local/bin/pip` – user2242044 Nov 05 '19 at 12:49
  • If you try `python -m pip install cython` with the ENV is set then you will get that cython is already installed – LinPy Nov 05 '19 at 12:50
  • Thank you @LinPy, as mentioned by Linpy you need to use `-m` flag. check the updated answer to work with alpine python packages along with pip. – Adiii Nov 05 '19 at 13:39