How to use virtualenv with python3.6 on ubuntu 16.04? - python

I'm using Ubuntu 16.04, which comes with Python 2.7 and Python 3.5. I've installed Python 3.6 on it and symlink python3 to python3.6 through alias python3=python3.6.
Then, I've installed virtualenv using sudo -H pip3 install virtualenv. When I checked, the virtualenv got installed in "/usr/local/lib/python3.5/dist-packages" location, so when I'm trying to create virtualenv using python3 -m venv ./venv1 it's throwing me errors:
Error Command: ['/home/wgetdj/WorkPlace/Programming/Python/myvenv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
What should I do?

We usually use $ python3 -m venv myvenv to create a new virtualenv (Here myvenv is the name of our virtualenv).
Similar to my case, if you have both python3.5 as well as python3.6 on your system, then you might get some errors.
NOTE: On some versions of Debian/Ubuntu you may receive the following error:
The virtual environment was not created successfully because ensure pip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command.
apt-get installpython3-venv
You may need to use sudo with that command. After installing the python3-venv package, recreate your virtual environment.
In this case, follow the instructions above and install the python3-venv package:
$ sudo apt-get install python3-venv
NOTE: On some versions of Debian/Ubuntu initiating the virtual environment like this currently gives the following error:
Error Command: ['/home/wgetdj/WorkPlace/Programming/Python/myvenv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
To get around this, use the virtualenv command instead.
$ sudo apt-get install python-virtualenv
$ virtualenv --python=python3.6 myvenv
NOTE: If you get an error like
E: Unable to locate package python3-venv
then instead run:
sudo apt install python3.6-venv

Installing python3.6 and python3.6-venv via ppa:deadsnakes/ppa instead of ppa:jonathonf/python-3.6 worked for me
apt-get update \
&& apt-get install -y software-properties-common curl \
&& add-apt-repository ppa:deadsnakes/ppa \
&& apt-get update \
&& apt-get install -y python3.6 python3.6-venv

First make sure you have python3.6 installed, otherwise you can install it with command:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt install python3.6
Now install venv i.e
sudo apt-get install python3.6-venv python3.6-dev
python3.6 -m venv venv_name
You can install python3.7/3.8 and also respective venv with above comman, just replace 3.6 with 3.X

I think that a problem could be related to the wrong locale.
I added to the /etc/environment the following lines to fix it:
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
You need to source the file from you bash with this command:
source /etc/environment

if you get following irritating error:
E: Unable to locate package python3-venv
try this commands:
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
those worked for me.hope it helps !

Related

Installed python3.9 not showing in linux

I installed python 3.9 following the steps in this link.
sudo apt update
sudo apt install python3.9
python3.9
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.[old-version] 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
sudo update-alternatives --config python3
However, it's throwing an error that python3.9 not found on the 3rd point. Also, I noticed the python3.9 on installation using the 2nd point is showing Note, selecting 'postgresql-plpython3-9.5' for regex 'python3.9'.
Complete message is
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'postgresql-plpython3-9.5' for regex 'python3.9'
The following packages were automatically installed and are no longer required:
linux-aws-headers-4.4.0-1104 linux-aws-headers-4.4.0-1105 linux-aws-headers-4.4.0-1106 linux-aws-headers-4.4.0-1107 linux-aws-headers-4.4.0-1109 linux-aws-headers-4.4.0-1110 linux-aws-headers-4.4.0-1111
linux-aws-headers-4.4.0-1112 linux-aws-headers-4.4.0-1113 linux-aws-headers-4.4.0-1114
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
postgresql-plpython3-9.5
0 upgraded, 1 newly installed, 0 to remove and 56 not upgraded.
Need to get 0 B/40.6 kB of archives.
After this operation, 166 kB of additional disk space will be used.
Selecting previously unselected package postgresql-plpython3-9.5.
(Reading database ... 362651 files and directories currently installed.)
Preparing to unpack .../postgresql-plpython3-9.5_9.5.25-0ubuntu0.16.04.1_amd64.deb ...
Unpacking postgresql-plpython3-9.5 (9.5.25-0ubuntu0.16.04.1) ...
Processing triggers for postgresql-common (173ubuntu0.3) ...
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
Removing obsolete dictionary files:
Setting up postgresql-plpython3-9.5 (9.5.25-0ubuntu0.16.04.1) ...
Why is it setting up postgresql-plpython3-9.5 and how can I prevent it from doing so?
The Problem:
The deadsnakes ppa is no longer available for Ubuntu Xenial. That's the reason you cannot install python3.9. See this issue. You will have to compile from source or upgrade your server to a supported version of Ubuntu.
Solution: build it yourself
If you are not able to upgrade your system, you could instead use pyenv to install any given python version as described below. For this a new version of openssl needs to be installed for python version >= 3.8
# install dependencies
apt update
apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev liblzma-dev git
# download and compile openssl
curl -L https://www.openssl.org/source/openssl-1.1.1s.tar.gz | (cd /usr/src; tar xz)
cd /usr/src/openssl-1.1.1s && ./config --prefix=/usr/local && make -j4 && make install
# download and configure pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
echo >> ~/.bashrc # add new-line.
echo 'export PATH="/root/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc
# build python 3.9.16 with pyenv
CONFIGURE_OPTS="--with-openssl=/usr/local --with-openssl-rpath=auto" pyenv install 3.9.16
# build python 3.10.9 with pyenv
CONFIGURE_OPTS="--with-openssl=/usr/local --with-openssl-rpath=auto" pyenv install 3.10.9
You may have it already installed
try running $ python3 --version
to see what python version your running.
If its not installed try running $ sudo apt-get update then run $ sudo apt-get install python3.9 to install python3.9
Hopefully this will help
You will need to add the deadsnakes repo.
sudo add-apt-repository ppa:deadsnakes/ppa
After that you can follow the steps in your question.
If you still have the same issues after adding the deadsnakes repo, it probably means your running an unsupported version of linux. You might then have to install Python 3.9 from source, you can check this answer on how to do that.

Update a raspberry pi to python 3.9

Been attempting to update a 2 year old, untouched raspberry pi to be able to run a pip module that only runs on 3.6+. I updated the operating system and I've been attempting to update python following this guide but I get to the last stage and it returns python 3.5.3. (note: i added the line to the bottom of the file because i couldn't find it)
I also tried
sudo update-alternatives --config python but it returned update-alternatives: error: no alternatives for python
First of all, try removing your current python version:
To know your version:
python --version
Then do this to remove Python:
sudo add-apt-repository --remove ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get remove --purge pythonX.X
And finally, enable your repositories again, and install Python 3.9:
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.9

cant install pip in ubuntu 18.04 docker /bin/sh: 1: pip: not found

I am getting the error using pip in my docker image.
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y \
software-properties-common
RUN add-apt-repository universe
RUN apt-get install -y \
python3.6 \
python3-pip
ENV PYTHONUNBUFFERED 1
RUN mkdir /api
WORKDIR /api
COPY . /api/
RUN pip install pipenv
RUN ls
RUN pipenv sync
I installed python 3.6 and pip3 but getting
Step 9/11 : RUN pip install pipenv
---> Running in b184de4eb28e
/bin/sh: 1: pip: not found
To run pip for python3 use pip3, not pip.
Another solution.
You can add this line (after apt-get install). It will upgrade pip to the version you need, for instance:
RUN pip3 install --upgrade pip==20.0.1
and you can then use pip install from requirements file (for instance):
RUN pip install -r requirements.txt

How to check whether paho mqtt is installed

I am running an .sh file and within that I am installing pip and paho-mqtt. I am running the file in ubuntu. But when I run the file for the second time also the pip and paho installation in happening. I want to check whether those are installed before executing these lies. Can someone help me with this.
My file is as follows,
#install mqtt dependency
git clone git://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.python.git
cd org.eclipse.paho.mqtt.python
sudo python setup.py install
sudo apt install python-pip
sudo pip install paho-mqtt
What i want to do is,
if !(check is installed) then
#install mqtt dependency
git clone git://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.python.git
cd org.eclipse.paho.mqtt.python
sudo python setup.py install
sudo apt install python-pip
sudo pip install paho-mqtt
This helped me
s=`dpkg -s python-pip | grep Status`
if [[ $s == *"installed"* ]]; then
#installed
else
git clone git://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.python.git
cd org.eclipse.paho.mqtt.python
sudo python setup.py install
sudo apt install python-pip
fi

Docker: "Unknown instruction: VIRTUALENV'

Dockerfile:
FROM ubuntu:14.04.2
RUN apt-get -y update && apt-get upgrade -y
RUN apt-get install python build-essential python-dev python-pip python-setuptools -y
RUN apt-get install libxml2-dev libxslt1-dev python-dev -y
RUN apt-get install libpq-dev postgresql-common postgresql-client -y
RUN apt-get install openssl openssl-blacklist openssl-blacklist-extra -y
RUN apt-get install nginx -y
RUN pip install virtualenv uwsgi
ADD canonicaliser_api ~
virtualenv ~/canonicaliser_api/venv
source ~/canonicaliser_api/venv/bin/activate
pip install -r ~/canonicaliser_api/requirements.txt
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80
CMD service nginx start
Build error:
...
Successfully installed virtualenv uwsgi
Cleaning up...
---> 0c141e23f725
Removing intermediate container d9fd3c20365d
Step 8 : ADD canonicaliser_api ~
---> 89b4fb40dba5
Removing intermediate container b0c1ad946fc4
Step 9 : VIRTUALENV
Unknown instruction: VIRTUALENV
is it supposed to remove those containers?
Why isn't it seeing virtualenv?
is it supposed to remove those containers?
Yes. If you want to keep them for some reason, pass --rm=false to the docker build command.
Why isn't it seeing virtualenv?
It is seeing it, but because it's at the start of a line, it treats it like a Dockerfile instruction, but there is no "VIRTUALENV" instruction. Presumably, you meant to put RUN before each line after the ADD:
ADD canonicaliser_api ~
RUN virtualenv ~/canonicaliser_api/venv
# This one needs to be a single RUN so the "source" will affect pip.
RUN source ~/canonicaliser_api/venv/bin/activate && \
pip install -r ~/canonicaliser_api/requirements.txt

Categories

Resources