There seems to be several ways to get to the same error, I'm describing most typical.
I've compiled python 3.6.2 on shared hosting server where I have no sudo rights (nevertheless it works nicely in cgi-bin). Later I've found it also happens on Ubuntu 16.04. I'm trying:
my-python3-path/bin/python3 -m venv my-venv-folder
This fails at the end with
Error: Command '['...my-venv-folder/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
I can try another way:
my-python3-path/bin/python3 -m venv --without-pip my-venv-folder
which succeeds, and then
my-venv-folder/bin/python -m ensurepip
(or with more options as suggested by result above) - it anyway ends with:
OSError: [Errno 22] Invalid argument: '/tmp/tmpt2cathtr'
and somewhat above the stacktrace ending with:
PermissionError: [Errno 13] Permission denied: '/tmp/pip-build-exot00s_'
Though in this way all necessary scripts (e.g. activate) are created in venv before pip, and pip files themselves seem to be present and working, until I really try install anything. Then the similar error exists.
Changing TMPDIR doesn't seem to help - the same /tmp folder is used.
The same or similar error occurred during make install by the way...
Subfolders in question (ones in /tmp have d-w------- access rights, so no wonder they couldn't be accessed).
If anyone encountered this problem, please share if you have find solution to get rid of it (though at present state the last variant looks like workaround), for I failed to find the solution with google. Sorry if description is not very coherent, for I'm not a python man and various attempts and googling made me a bit dizzy. Thanks in advance!
UPD: it seems to happen that temporary directories are created with rwx rights for user, but later the mode becomes -w- and rmtree fails. current workaround seems to be comment out rmtree calls in such places (several of them)...