Output of Conv op in onnx is of Shape None - onnx

I trained a pyTorch model and tried to export as onnx format. Script I used
!git clone https://github.com/WongKinYiu/yolov7
!pip install -r yolov7/requirements.txt
...
!python3 yolov7/export.py --weights {my_checkpoint_path} --grid --simplify
Then, when I tried to export to tf using onnx2tf, I got
Model convertion started ============================================================
INFO: input_op_name: images shape: [1, 3, 640, 640] dtype: float32
INFO: onnx_op_type: Conv onnx_op_name: /model.0/conv/Conv
INFO: input_name.1: images shape: [1, 3, 640, 640] dtype: float32
INFO: input_name.2: model.0.conv.weight shape: [32, 3, 3, 3] dtype: <class 'numpy.float32'>
INFO: input_name.3: model.0.conv.bias shape: [32] dtype: <class 'numpy.float32'>
INFO: output_name.1: /model.0/conv/Conv_output_0 shape: None dtype: None
ERROR: The trace log is below.
Traceback (most recent call last):
File "/usr/local/google/home/huiliukeith/pytorchenv/lib/python3.10/site-packages/onnx2tf/utils/common_functions.py", line 267, in print_wrapper_func
result = func(*args, **kwargs)
File "/usr/local/google/home/huiliukeith/pytorchenv/lib/python3.10/site-packages/onnx2tf/utils/common_functions.py", line 329, in inverted_operation_enable_disable_wrapper_func
result = func(*args, **kwargs)
File "/usr/local/google/home/huiliukeith/pytorchenv/lib/python3.10/site-packages/onnx2tf/ops/Conv.py", line 111, in make_node
and graph_node.inputs[0].shape[2:] == output_tensor_shape[2:]:
TypeError: 'NoneType' object is not subscriptable
ERROR: Read this and deal with it. https://github.com/PINTO0309/onnx2tf#parameter-replacement
ERROR: Alternatively, if the input OP has a dynamic dimension, use the -b or -ois option to rewrite it to a static shape and try again.
ERROR: If the input OP of ONNX before conversion is NHWC, use the -kt option.
I feel like it is not of onnx2tf issue because I do see examples where output shape of Conv is not None (https://github.com/PINTO0309/onnx2tf/issues/15)?
If so, is there some options when using torch.onnx.export that might cause the output shape to be None?
Thanks!

Assuming you are using Google Colaboratory, here are the commands you were able to convert successfully.You will probably need to specify python3.9 and run the command.
Also, if onnxsim is installed correctly, the error you have encountered will not occur. So I dared not use yolov7's requirements.txt and installed only the minimum required packages.
!sudo add-apt-repository -y ppa:deadsnakes/ppa
!sudo apt-get -y update
!sudo apt-get -y install python3.9
!sudo apt-get -y install python3.9-dev
!sudo apt-get -y install python3-pip
!sudo apt-get -y install python3.9-distutils
!python3.9 -m pip install -U setuptools \
&& python3.9 -m pip install -U pip \
&& python3.9 -m pip install -U distlib
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
!python3.9 -m pip install tensorflow==2.10.0 \
&& python3.9 -m pip install -U onnx \
&& python3.9 -m pip install -U nvidia-pyindex \
&& python3.9 -m pip install -U onnx-graphsurgeon \
&& python3.9 -m pip install -U onnxsim \
&& python3.9 -m pip install -U simple_onnx_processing_tools \
&& python3.9 -m pip install -U onnx2tf \
&& python3.9 -m pip install -U protobuf==3.20.3 \
&& python3.9 -m pip install -U h5py==3.7.0
!python3.9 -m pip install -U \
torch \
torchvision \
pandas \
opencv-python \
tqdm \
pyyaml \
matplotlib \
seaborn \
scipy \
coremltools \
onnxruntime
!git clone https://github.com/WongKinYiu/yolov7
%cd yolov7
!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt
!python3.9 export.py --weights yolov7.pt --grid --simplify
!onnx2tf -i yolov7.onnx -cotof -cotoa 1e-1
Note that the output from ONNX and TensorFlow match perfectly.
INFO: onnx_output_name: /model.105/Mul_5_output_0 shape: (1, 3, 20, 20, 2) dtype: float32 validate_result: Matches
INFO: onnx_output_name: /model.105/Concat_2_output_0 shape: (1, 3, 20, 20, 85) dtype: float32 validate_result: Matches
INFO: onnx_output_name: /model.105/Reshape_5_output_0 shape: (1, 1200, 85) dtype: float32 validate_result: Matches
INFO: onnx_output_name: output shape: (1, 25200, 85) dtype: float32 validate_result: Matches

Related

Unable to find libcrypt.so.1 while importing ibm_db

I am getting following traceback while importing ibm_db in python -
Traceback (most recent call last):
File "/working/Script.py", line 5, in <module>
import ibm_db
ImportError: Error loading shared library libcrypt.so.1: No such file or directory (needed by
/usr/local/lib/python3.9/site-packages/clidriver/lib/libdb2.so.1)
Please find below my docker image and requirements.txt file -
FROM python:3.9-alpine3.16
COPY requirements.txt requirements.txt
RUN apk --update add --virtual build-dependencies python3 py-pip openssl ca-certificates py-openssl wget libffi-dev openssl-dev python3-dev py-pip py3-pandas build-base \
&& apk add python3 make g++ \
&& pip install --upgrade pip \
&& pip install -r requirements.txt \
&& apk del build-dependencies
ENTRYPOINT [ "python", "Script.py" ]
Requirements.txt -
cryptography==2.9
botocore==1.12.253
azure-storage-blob==2.1.0
azure-storage-common==2.1.0
snowflake-connector-python==1.9.1
snowflake-sqlalchemy==1.2.4
SQLAlchemy==1.3.19
ibm_db
s3fs
The IBM clidriver (on which the python ibm_db module depends) is not supported on Alpine-Linux - that is to say, it will not work on Alpine-Linux.
That driver (clidriver) is closed source, and for Linux x64 is built with gcc.
You must choose a supported base Linux , Ubuntu, Red Hat, SuSe.

libcrypto not found using oscrypto library in Alpine-based Docker image

I am getting exactly the same error as mentioned in ->
The library libcrypto could not be found
I understood the issue, however, I could not figure out the resolution. Do I need to update my lambda configuration or do I need to upgrade my Python libraries?
PFB my requriements.txt files
cryptography==36.0.2
botocore==1.20.0
azure-storage-blob==2.1.0
azure-storage-common==2.1.0
boto3==1.17.0
asn1crypto==1.5.1
certifi==2022.9.14
cffi==1.15.1
charset-normalizer==2.1.1
filelock==3.8.0
idna==3.4
oscrypto==1.3.0
pycparser==2.21
pycryptodomex==3.15.0
PyJWT==2.5.0
pyOpenSSL==22.0.0
pytz==2022.2.1
requests==2.28.1
typing_extensions==4.3.0
urllib3==1.26.12
My docker file -
FROM python:3.9-alpine3.16
COPY requirements.txt requirements.txt
RUN apk --update --no-cache add --virtual build-dependencies gcc python3-dev musl-dev libc-dev linux-headers libxslt-dev libxml2-dev py-pip ca-certificates wget libffi-dev openssl-dev python3-dev expat==2.4.9-r0 py-pip build-base zlib zlib-dev libressl libressl-dev \
&& apk add python3 make g++ \
&& pip install --upgrade pip \
&& pip install --upgrade pip setuptools \
&& pip install -r requirements.txt \
&& apk del build-dependencies
RUN pip install snowflake-connector-python==2.8.0 --no-use-pep517
RUN python -c 'from oscrypto import asymmetric'
Attempting docker build with the Dockerfile above results in a failure with:
Step 4/4 : RUN python -c 'from oscrypto import asymmetric'
---> Running in dc8f8b8920ac
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python3.9/site-packages/oscrypto/asymmetric.py", line 19, in <module>
from ._asymmetric import _unwrap_private_key_info
File "/usr/local/lib/python3.9/site-packages/oscrypto/_asymmetric.py", line 27, in <module>
from .kdf import pbkdf1, pbkdf2, pkcs12_kdf
File "/usr/local/lib/python3.9/site-packages/oscrypto/kdf.py", line 9, in <module>
from .util import rand_bytes
File "/usr/local/lib/python3.9/site-packages/oscrypto/util.py", line 14, in <module>
from ._openssl.util import rand_bytes
File "/usr/local/lib/python3.9/site-packages/oscrypto/_openssl/util.py", line 6, in <module>
from ._libcrypto import libcrypto, libcrypto_version_info, handle_openssl_error
File "/usr/local/lib/python3.9/site-packages/oscrypto/_openssl/_libcrypto.py", line 9, in <module>
from ._libcrypto_cffi import (
File "/usr/local/lib/python3.9/site-packages/oscrypto/_openssl/_libcrypto_cffi.py", line 27, in <module>
raise LibraryNotFoundError('The library libcrypto could not be found')
oscrypto.errors.LibraryNotFoundError: The library libcrypto could not be found
The command '/bin/sh -c python -c 'from oscrypto import asymmetric'' returned a non-zero code: 1
The exception is taking place at https://github.com/wbond/oscrypto/blob/1.3.0/oscrypto/_openssl/_libcrypto_cffi.py
Tracking through oscrypto._ffi, the problem comes down to an issue opening libcrypto with the ctypes library:
>>> import ctypes.util
>>> ctypes.util.find_library('crypto')
>>>
Why? Because /usr/lib has only libcrypto.so.1.1, and not a libcrypto.so symlink pointing to it. Easily fixed by adding an extra line to your Dockerfile:
RUN ln -s libcrypto.so.1.1 /usr/lib/libcrypto.so
...after which Python's ctypes behaves:
>>> from ctypes.util import find_library
>>> find_library('crypto')
'libcrypto.so.1.1'
...and so does oscrypto:
>>> from oscrypto import asymmetric
>>>

using package compiled with nvcc and installed with setup.py with remote interpreter

I use the remote interpreter of my docker container.
The Dockerfile contents are:
FROM nvidia/cuda:8.0-cudnn7-devel-ubuntu16.04
RUN apt-get update && apt-get install -y \
rsync \
htop \
git \
openssh-server \
nano \
cmake \
python-opencv \
python2.7 \
unzip
RUN apt-get -qq -y install curl bzip2 && \
curl -sSL https://repo.continuum.io/miniconda/Miniconda2-4.6.14-Linux-x86_64.sh -o /tmp/miniconda.sh && \
bash /tmp/miniconda.sh -bfp /usr/local && \
rm -rf /tmp/miniconda.sh && \
conda install -y python=2 && \
conda update conda && \
apt-get -qq -y autoremove && \
apt-get autoclean && \
rm -rf /var/lib/apt/lists/* /var/log/dpkg.log && \
conda clean --all --yes
#RUN . usr/local/etc/profile.d/conda.sh #didnt work
#RUN conda create -n pwcnet_test python=2.7
#RUN chmod u+r+x usr/local/etc/profile.d/conda.sh
#RUN conda env list
#RUN usr/local/etc/profile.d/conda.sh activate pwcnet_test
# install pytorch and other dependencies
#RUN pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl
#RUN pip install torchvision visdom dominate opencv-python cffi
# install external packages
ADD torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl
RUN pip install torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl
#RUN conda install pytorch=0.2.0 cuda80 -c soumith
RUN pip install torchvision==0.2.2 visdom==0.1.8.8 dominate==2.3.5 opencv-python==4.1.0.25 cffi==1.12.2
ADD external_packages external_packages
RUN bash /external_packages/correlation-pytorch-master/make_cuda.sh
#RUN mkdir /usr/local/lib/python2.7/site-packages/correlation
#RUN cp /usr/local/lib/python2.7/site-packages/correlation_package-0.1-py2.7-linux-x86_64.egg /usr/local/lib/python2.7/site-packages/correlation_package-0.1-py2.7-linux-x86_64.zip
#RUN unzip /usr/local/lib/python2.7/site-packages/correlation_package-0.1-py2.7-linux-x86_64.zip -d /usr/local/lib/python2.7/site-packages/correlation
ENV NVIDIA_VISIBLE_DEVICES all
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
The content of make_cuda.sh is:
#!/usr/bin/env bash
CUDA_PATH=/usr/local/cuda-8.0
#cd correlation-pytorch/correlation_package/src
echo "Compiling correlation layer kernels by nvcc..."
# TODO (JEB): Check which arches we need
nvcc -c -o /external_packages/correlation-pytorch-master/correlation-pytorch/correlation_package/src/corr_cuda_kernel.cu.o /external_packages/correlation-pytorch-master/correlation-pytorch/correlation_package/src/corr_cuda_kernel.cu -x cu -Xcompiler -fPIC -arch=sm_52
nvcc -c -o /external_packages/correlation-pytorch-master/correlation-pytorch/correlation_package/src/corr1d_cuda_kernel.cu.o /external_packages/correlation-pytorch-master/correlation-pytorch/correlation_package/src/corr1d_cuda_kernel.cu -x cu -Xcompiler -fPIC -arch=sm_52
#cd ../../
python /external_packages/correlation-pytorch-master/correlation-pytorch/setup.py build install
The image is created successfully and the interpreter works great (in run and debug).
if I run pip list, I can see the package correlation-package 0.1 appears in the list of installed modules.
If I look at the the packages-site content I see and egg file: correlation_package-0.1-py2.7-linux-x86_64.egg.
I can also notice that if I run 'python -m site', in the output appears '/usr/local/lib/python2.7/site-packages/correlation_package-0.1-py2.7-linux-x86_64.egg'.
But if I try to imp.find_module('correlation-package')[1], it write to me that:
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: No module named correlation-package
It is also doesn't recognize it if I try to simply import a class from the module:
from correlation_package.modules.corr import Correlation
returns:
usr/local/bin/python2.7 /opt/.pycharm_helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 172.17.0.1 --port 45203 --file /opt/project/models/PWCNet.py
Connected to pydev debugger (build 222.3739.30)
pydev debugger: warning: trying to add breakpoint to file that does not exist: /home/vista/.cache/JetBrains/PyCharm2022.2/remote_sources/-609766439/-183260405/correlation/correlation_package/_ext/corr/_corr.py (will have no effect)
pydev debugger: warning: trying to add breakpoint to file that does not exist: /home/vista/.cache/JetBrains/PyCharm2022.2/remote_sources/-609766439/-183260405/correlation/correlation_package/_ext/corr/_corr.py (will have no effect)
Traceback (most recent call last):
File "/opt/.pycharm_helpers/pydev/pydevd.py", line 1496, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "/opt/project/models/PWCNet.py", line 13, in <module>
from correlation_package.modules.corr import Correlation
ImportError: No module named correlation_package.modules.corr
python-BaseException
and of course it doesn't see it in the file itself:
You can see in the Dockerfile in the commented commands that I tried to unzip the files in the .egg archive of the module and import it using imp.load_source as following:
Correlation = imp.load_source('Correlation', '/usr/local/lib/python2.7/site-packages/correlation/correlation_package/_ext/corr/_corr.py')
but this ended with the error:
raceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/correlation/correlation_package/_ext/corr/_corr.py", line 7, in <module>
__bootstrap__()
File "/usr/local/lib/python2.7/site-packages/correlation/correlation_package/_ext/corr/_corr.py", line 6, in __bootstrap__
imp.load_dynamic(__name__,__file__)
ImportError: /usr/local/lib/python2.7/site-packages/correlation/correlation_package/_ext/corr/_corr.so: undefined symbol: __gxx_personality_v0
if I run this command from the python console and a different error pop up from debugging this line in the debugger of pycharm:
/usr/local/bin/python2.7 /opt/.pycharm_helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 172.17.0.1 --port 41629 --file /opt/project/models/PWCNet.py
Connected to pydev debugger (build 222.3739.30)
pydev debugger: warning: trying to add breakpoint to file that does not exist: /home/vista/.cache/JetBrains/PyCharm2022.2/remote_sources/-609766439/-183260405/correlation/correlation_package/_ext/corr/_corr.py (will have no effect)
pydev debugger: warning: trying to add breakpoint to file that does not exist: /home/vista/.cache/JetBrains/PyCharm2022.2/remote_sources/-609766439/-183260405/correlation/correlation_package/_ext/corr/_corr.py (will have no effect)
Traceback (most recent call last):
File "/opt/.pycharm_helpers/pydev/pydevd.py", line 1496, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "/opt/project/models/PWCNet.py", line 17, in <module>
Correlation = imp.load_source('Correlation', '/usr/local/lib/python2.7/site-packages/correlation/correlation_package/_ext/corr/_corr.py')
File "/usr/local/lib/python2.7/site-packages/correlation/correlation_package/_ext/corr/_corr.py", line 7, in <module>
__bootstrap__()
File "/usr/local/lib/python2.7/site-packages/correlation/correlation_package/_ext/corr/_corr.py", line 6, in __bootstrap__
imp.load_dynamic(__name__,__file__)
ImportError: dynamic module does not define init function (initCorrelation)
python-BaseException
as the contents of the corr.py file are the following:
def __bootstrap__():
global __bootstrap__, __loader__, __file__
import sys, pkg_resources, imp
__file__ = pkg_resources.resource_filename(__name__, '_corr.so')
__loader__ = None; del __bootstrap__, __loader__
imp.load_dynamic(__name__,__file__)
__bootstrap__()
and the contents of the unzipped egg /correlation/correlation_package/_ext/corr/ are:
_corr.py
_corr.pyc
_corr.so
the debug of this load_source operation shows that the imp.load_dynamic in the bootstrap function runs as follows:
What is the correct way to import a package which is on the docker container environment ?
Thanks in advance.
P.S:
If I add to the Dockerfile the line RUN python /external_packages/correlation-pytorch-master/correlation-pytorch/test/test.py which is made to test the package installation, I get the error:
Step 9/14 : RUN python /external_packages/correlation-pytorch-master/correlation-pytorch/test/test.py
---> Running in c57c673c66c1
Traceback (most recent call last):
File "/external_packages/correlation-pytorch-master/correlation-pytorch/test/test.py", line 4, in <module>
from correlation_package.modules.corr import Correlation, Correlation1d
ImportError: No module named correlation_package.modules.corr
This is confusing because as was said above pip list does shows it int he installed packages list. This is totally executed from inside the container, as you can see below:
Step 9/15 : RUN pip list
---> Running in adbe3ba085b2
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 wont be maintained after that date. A future version of pip will drop support for Python 2.7.
Package Version
---------------------- -----------
asn1crypto 1.4.0
backports-abc 0.5
certifi 2020.6.20
cffi 1.12.2
chardet 3.0.4
conda 4.8.4
conda-package-handling 1.6.0
correlation-package 0.1
cryptography 2.6.1
dominate 2.3.5
enum34 1.1.6
futures 3.3.0
idna 2.8
ipaddress 1.0.23
numpy 1.16.6
opencv-python 4.1.0.25
Pillow 6.2.2
pip 19.0.3
pycosat 0.6.3
pycparser 2.19
pyOpenSSL 19.0.0
PySocks 1.7.1
PyYAML 5.4.1
pyzmq 19.0.2
requests 2.21.0
ruamel-yaml 0.15.46
scandir 1.10.0
scipy 1.2.3
setuptools 41.0.0
singledispatch 3.7.0
six 1.16.0
torch 0.2.0.post3
torchfile 0.1.0
torchvision 0.2.2
tornado 5.1.1
tqdm 4.19.9
urllib3 1.24.1
visdom 0.1.8.8
websocket-client 0.59.0
wheel 0.37.1
Removing intermediate container adbe3ba085b2
---> 120baf3ba68a
Step 10/15 : RUN python /external_packages/correlation-pytorch-master/correlation-pytorch/test/test.py
---> Running in e28c04ca8d12
Traceback (most recent call last):
File "/external_packages/correlation-pytorch-master/correlation-pytorch/test/test.py", line 4, in <module>
from correlation_package.modules.corr import Correlation, Correlation1d
ImportError: No module named correlation_package.modules.corr

Delta Lake on EMR and Zeppelin gives 'configure_spark_with_delta_pip' is not defined

I am trying to use Delta lake on Zeppelin running on EMR. Below is my simple bootstrap script, I am using spark-delta 0.0.1 as spark version on EMR is 2.4.4. When I try to create spark session in notebook I below exception.
Fail to execute line 7: spark = configure_spark_with_delta_pip(builder).getOrCreate()
Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-2848864742877081666.py", line 380, in
exec(code, _zcUserQueryNameSpace)
File "", line 7, in
NameError: name 'configure_spark_with_delta_pip' is not defined
I also tried adding delta-code_2.11-0.6.1.jar before creating the session but not luck and got below exception (shown in screenshot).
module 'pyspark.sql.utils' has no attribute 'convert_exception'
#!/usr/bin/env bash
# Upgrade pip
python3 -m pip uninstall botocore --user
python3 -m pip uninstall boto3 --user
sudo python3 -m pip install --upgrade pip
python3 -m pip install botocore==1.13.38 --user
sudo yum -y install python3-devel
python3 -m pip install keras==2.2.4 --user
python3 -m pip install numpy==1.19.1 --user
sudo python3 -m pip install pyarrow
sudo python3 -m pip install boto3
sudo python3 -m pip install torch
sudo python3 -m pip install scipy
sudo python3 -m pip install pandas
sudo python3 -m pip install delta-spark==0.0.1
0.0.1 is a test version to verify pip release process. It's not a real release. You should use 1.0.0 or above based on your Spark version. You can find the compatibility matrix in the doc.
sudo python3 -m pip install delta-spark==1.0.0

How to install pip for Python 3.6 on Ubuntu 16.10?

I'd like to start by pointing out that this question may seem like a duplicate, but it isn't. All the questions I saw here were regarding pip for Python 3 and I'm talking about Python 3.6. The steps used back then don't work for Python 3.6.
I got a clear Ubuntu 16.10 image from the official docker store.
Run apt-get update
Run apt-get install python3.6
Run apt-get install python3-pip
Run pip3 install requests bs4
Run python3.6 script.py
Got ModuleNotFoundError below:
Traceback (most recent call last):
File "script.py", line 6, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
Python's and pip's I have in the machine:
python3
python3.5
python3.5m
python3.6
python3m
python3-config
python3.5-config
python3.5m-config
python3.6m
python3m-config
pip
pip3
pip3.5
Let's suppose that you have a system running Ubuntu 16.04, 16.10, or 17.04, and you want Python 3.6 to be the default Python.
If you're using Ubuntu 16.04 LTS, you'll need to use a PPA:
sudo add-apt-repository ppa:jonathonf/python-3.6 # (only for 16.04 LTS)
Then, run the following (this works out-of-the-box on 16.10 and 17.04):
sudo apt update
sudo apt install python3.6
sudo apt install python3.6-dev
sudo apt install python3.6-venv
wget https://bootstrap.pypa.io/get-pip.py
sudo python3.6 get-pip.py
sudo ln -s /usr/bin/python3.6 /usr/local/bin/python3
sudo ln -s /usr/local/bin/pip /usr/local/bin/pip3
# Do this only if you want python3 to be the default Python
# instead of python2 (may be dangerous, esp. before 2020):
# sudo ln -s /usr/bin/python3.6 /usr/local/bin/python
When you have completed all of the above, each of the following shell commands should indicate Python 3.6.1 (or a more recent version of Python 3.6):
python --version # (this will reflect your choice, see above)
python3 --version
$(head -1 `which pip` | tail -c +3) --version
$(head -1 `which pip3` | tail -c +3) --version
In at least in ubuntu 16.10, the default python3 is python3.5. As such, all of the python3-X packages will be installed for python3.5 and not for python3.6.
You can verify this by checking the shebang of pip3:
$ head -n1 $(which pip3)
#!/usr/bin/python3
Fortunately, the pip installed by the python3-pip package is installed into the "shared" /usr/lib/python3/dist-packages such that python3.6 can also take advantage of it.
You can install packages for python3.6 by doing:
python3.6 -m pip install ...
For example:
$ python3.6 -m pip install requests
$ python3.6 -c 'import requests; print(requests.__file__)'
/usr/local/lib/python3.6/dist-packages/requests/__init__.py
This answer assumes that you have python3.6 installed. For python3.7, replace 3.6 with 3.7. For python3.8, replace 3.6 with 3.8, but it may also first require the python3.8-distutils package.
Installation with sudo
With regard to installing pip, using curl (instead of wget) avoids writing the file to disk.
curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3.6
The -H flag is evidently necessary with sudo in order to prevent errors such as the following when installing pip for an updated python interpreter:
The directory '/home/someuser/.cache/pip/http' or its parent directory
is not owned by the current user and the cache has been disabled.
Please check the permissions and owner of that directory. If executing
pip with sudo, you may want sudo's -H flag.
The directory
'/home/someuser/.cache/pip' or its parent directory is not owned by the
current user and caching wheels has been disabled. check the
permissions and owner of that directory. If executing pip with sudo,
you may want sudo's -H flag.
Installation without sudo
curl https://bootstrap.pypa.io/get-pip.py | python3.6 - --user
This may sometimes give a warning such as:
WARNING: The script wheel is installed in '/home/ubuntu/.local/bin'
which is not on PATH. Consider adding this directory to PATH or, if
you prefer to suppress this warning, use --no-warn-script-location.
Verification
After this, pip, pip3, and pip3.6 can all be expected to point to the same target:
$ (pip -V && pip3 -V && pip3.6 -V) | uniq
pip 18.0 from /usr/local/lib/python3.6/dist-packages (python 3.6)
Of course you can alternatively use python3.6 -m pip as well.
$ python3.6 -m pip -V
pip 18.0 from /usr/local/lib/python3.6/dist-packages (python 3.6)
This website contains a much cleaner solution, it leaves pip intact as-well and one can easily switch between 3.5 and 3.6 and then whenever 3.7 is released.
http://ubuntuhandbook.org/index.php/2017/07/install-python-3-6-1-in-ubuntu-16-04-lts/
A short summary:
sudo apt-get install python python-pip python3 python3-pip
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
Then
$ pip -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
$ pip3 -V
pip 8.1.1 from /usr/local/lib/python3.5/dist-packages (python 3.5)
Then to select python 3.6 run
sudo update-alternatives --config python3
and select '2'. Then
$ pip3 -V
pip 8.1.1 from /usr/local/lib/python3.6/dist-packages (python 3.6)
To update pip select the desired version and
pip3 install --upgrade pip
$ pip3 -V
pip 9.0.1 from /usr/local/lib/python3.6/dist-packages (python 3.6)
Tested on Ubuntu 16.04.
Some of the solutions above using the script get-pip.py worked until a couple of weeks ago.
The latest version of this script now requires python3.7 throwing the following error
ERROR: This script does not work on Python 3.6
The minimun supported Python version is 3.7.
Please use https://bootstrap.pypa.io/pip/3.6/get-pip.py instead.
So making the corresponding change works now.
wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
sudo python3.6 get-pip.py

Resources