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'
Advertisement
Answer
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 toPYTHONPATH
.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 variablesys.path
.
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.
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"