Isolate virtual environment dependecies from system-installed python on ubuntu 22.04 - python

I'm on ubuntu 22.04 and installed pip using Linux package managers:
sudo apt update
sudo apt install python3-venv python3-pip
The point is that when I activate virtual environment and install dependencies on it:
source venv/bin/activate
pip install -r requirements.txt
, it installed on system-installed python rather than on a virtual environment.
which pip
output is: /usr/bin/pip which is the same folder without the virtual environment:
deactivate
which pip
the output is : /usr/bin/pip
the objective is to install the package only on the environment separately from system-installed python

in directory of your project use this command to create virtual environment
python3 -m venv venv
if you wish to use other version of python first you should install it self and venv module with following command
sudo apt update && sudo apt install python3.9 python3.9-venv python3.9-pip
to use venv first you need to activate it with:
source venv/bin/activate
or
. venv/bin/activate
then use venv python with this command
pyhton3.9 -m pip install -r requirements.txt

Related

Is it safe to create python virtualenv to /usr/share/?

I've been working on building debian package of my project.
When user installs my package, then my project's binaries are installed on /usr/bin/*. And then, a bash script is invoked at the end that creates python virtual environment to /usr/share/my_proejct/venv and installs required python package on that virtual environment.
$ sudo dpkg -i my_project.deb
# being installed on /usr/bin/*
# automatically `sudo post_install.sh` is invoked(debian postinst)
$ cat post_install.sh
python3 -m pip install -U virtualenv # sudo
python3 -m venv /usr/share/my_project/venv # sudo
/usr/share/my_project/venv/bin/python -m pip install ${REQUIRED_PACKAGES}
And my project's binaries are using that virtual environment's python.
AFAIK, running pip with sudo has security problem. But I just use virtualenv's python( /usr/share/my_project/venv/bin/python) directly;I still install virtualenv with sudo and create venv with it. Is it still dangerous?
Can I use this virtual environment's python with multiple users?

How to Install pip for python 3.7 on Ubuntu 18?

I've installed Python 3.7 on my Ubuntu 18.04 machine. Following this instructions in case it's relevant:
Download : Python 3.7 from Python Website [1] ,on Desktop and manually
unzip it, on Desktop Installation : Open Terminal (ctrl +shift+T)
Go to the Extracted folder
$ cd ~/Desktop/Python-3.7.0
$ ./configure
$ make
$ sudo make install
Making Python 3.7 default Python :
$ sudo vim ~/.bashrc
press i
on the last and new line - Type
alias python= python3.7
press Esc
type - to save and exit vim
:wq
now type
$ source ~/.bashrc
From here: https://www.quora.com/How-can-I-upgrade-Python-3-6-to-3-7-in-Ubuntu-18-04
I've downloaded several modules through pip install module but when I try to import them, I get a ModuleNotFoundError: No module names 'xx'
So I did some research and apparently when used pip to install, it installed in the modules in previous version of Python.
Somewhere (probably a question in SO) I found a suggestion to install the module using python3.7 -m pip install module but then I get /usr/local/bin/python3.7: no module named pip.
Now I'm stuck, pip is installed, but apparently not for Python 3.7. I'm assuming that if I can install pip for Python 3.7, I can run the pip install command and get the modules I need.
If that is the case, how can I install pip for python 3.7, since it's already installed?
This is the best I have come up with:
I have installed python 3.7 successfully and I can install modules using pip (or pip3) but those modules are installed in Python 3.6 (Comes with ubuntu). Therefore I can't import those modules in python 3.7 (get a module not found)
Python 3.7 doesn't recognize pip/pip3, so I can't install through pip/pip3
I need python 3.7
In general, don't do this:
pip install package
because, as you have correctly noticed, it's not clear what Python version you're installing package for.
Instead, if you want to install package for Python 3.7, do this:
python3.7 -m pip install package
Replace package with the name of whatever you're trying to install.
Took me a surprisingly long time to figure it out, too. The docs about it are here.
Your other option is to set up a virtual environment. Once your virtual environment is active, executable names like python and pip will point to the correct ones.
A quick add-on to mpenkov's answer above (didn't want this to get lost in the comments)
For me, I had to install pip for 3.6 first
sudo apt install python3-pip
now you can install python 3.7
sudo apt install python3.7
and then I could install pip for 3.7
python3.7 -m pip install pip
and as a bonus, to install other modules just preface with
python3.7 -m pip install <module>
EDIT 1 (12/2019):
I know this is obvious for most. but if you want python 3.8, just substitute python3.8 in place of python3.7
EDIT 2 (5/2020):
For those that are able to upgrade, Python 3.8 is available out-of-the-box for Ubuntu 20.04 which was released a few weeks ago.
This works for me.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Then this command with sudo:
python3.7 get-pip.py
Based on this instruction.
I used apt-get to install python3.7 in ubuntu18.04. The installations are as follows.
install python3.7
sudo apt-get install python3.7
install pip3. It should be noted that this may install pip3 for python3.6.
sudo apt-get install python3-pip
change the default of python3 for python3.7. This is where the magic is, which will make the pip3 refer to python3.7.
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
Hope it works for you.
To install all currently supported python versions (python 3.6 is already pre-installed) including pip for Ubuntu 18.04 do the following:
To install python3.5 and python3.7, use the deadsnakes ppa:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.5
sudo apt-get install python3.7
Install python2.7 via distribution packages:
sudo apt install python-minimal # on Ubuntu 18.04 python-minimal maps to python2.7
To install pip use:
sudo apt install python-pip # on Ubuntu 18.04 this refers to pip for python2.7
sudo apt install python3-pip # on Ubuntu 18.04 this refers to pip for python3.6
python3.5 -m pip install pip # this will install pip only for the current user
python3.7 -m pip install pip
I used it for setting up a CI-chain for a python project with tox and Jenkins.
Combining the answers from #mpenkon and #dangel, this is what worked for me:
sudo apt install python3-pip
python3.7 -m pip install pip
Step #1 is required (assuming you don't already have pip for python3) for step #2 to work. It uses pip for Python3.6 to install pip for Python 3.7 apparently.
When i use apt install python3-pip, i get a lot of packages need install, but i donot need them. So, i DO like this:
apt update
apt-get install python3-setuptools
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
rm -f get-pip.py
The following steps can be used:
sudo apt-get -y update
---------
sudo apt-get install python3.7
--------------
python3.7
-------------
curl -O https://bootstrap.pypa.io/get-pip.py
-----------------
sudo apt install python3-pip
-----------------
sudo apt install python3.7-venv
-----------------
python3.7 -m venv /home/ubuntu/app
-------------
cd app
----------------
source bin/activate
Install python pre-requisites
sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Install python 3.7 (from ppa repository)
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.7
Install pip3.7
sudo apt install python3-pip
python3.7 -m pip install pip
Create python and pip alternatives
sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python3.7 10
sudo update-alternatives --install /usr/local/bin/pip pip /home/your_username/.local/bin/pip3.7 10
Make changes
source ~/.bashrc
python --version
pip --version
For those who intend to use venv:
If you don't already have pip for Python 3:
sudo apt install python3-pip
Install venv package:
sudo apt install python3.7-venv
Create virtual environment (which will be bootstrapped with pip by default):
python3.7 -m venv /path/to/new/virtual/environment
To activate the virtual environment, source the appropriate script for the current shell, from the bin directory of the virtual environment. The appropriate scripts for the different shells are:
bash/zsh – activate
fish – activate.fish
csh/tcsh – activate.csh
For example, if using bash:
source /path/to/new/virtual/environment/bin/activate
Optionally, to update pip for the virtual environment (while it is activated):
pip install --upgrade pip
When you want to deactivate the virtual environment:
deactivate
I installed pip3 using
python3.7 -m pip install pip
But upon using pip3 to install other dependencies, it was using python3.6.
You can check the by typing pip3 --version
Hence, I used pip3 like this (stated in one of the above answers):
python3.7 -m pip install <module>
or use it like this:
python3.7 -m pip install -r requirements.txt
I made a bash alias for later use in ~/.bashrc file as alias pip3='python3.7 -m pip'. If you use alias, don't forget to source ~/.bashrc after making the changes and saving it.
How about simply
add-apt-repository ppa:deadsnakes/ppa
apt-get update
apt-get install python3.7-dev
alias pip3.7="python3.7 -m pip"
Now you have the command
pip3.7
separately from pip3.
curl https://bootstrap.pypa.io/get-pip.py | sudo python3.7
if all else fails.
pip3 not pip. You can create an alias like you did with python3 if you like.

How to install a module to one of many pythons in Fedora 20

I have on my Fedora 20 additionally to 2.7 a python3.6 version installed.
When I run a script with the 3.6 version it's missing the requests module.
When I try to install it with the pip command it says it's already there.
So, how can I install this module in python3.6?
Any hints?
Thanks
Check if pip36 or more likely pip3 is a function you can run. Often times the pip command corresponds to the first installed python version, so if you install one later it gets the suffix according to its version. If that is the case then you'll want to do pip36 (pip3) install moduleXYZ.
Quick and dirty answer is
For python2,x install any package using pip install requests
For python 3.x install any package using pip3 install requests
If you get error during pip3 please run sudo dnf install python3-pip
But the right way is to do it using Virtual Environment in fedona
For py3.4+
$ python3.5 -m venv env # create the virtualenv
$ . env/bin/activate # activate it
(env)$ python -m pip install requests # install a package with pip
For py2.x, 3.x
$ dnf install python-virtualenv # install the necessary tool
$ virtualenv --python /usr/bin/python2.7 env # create the virtualenv
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in env/bin/python2.7
Also creating executable in env/bin/python
Installing setuptools, pip...done.
$ . env/bin/activate # activate it
(env)$ python -m pip install requests # install a package with pip
Refrence

pyvenv not working because ensurepip is not available

I upgraded from ubuntu 14.04 to ubuntu 16.04 a few days ago.
When I try to create a virtual env by using
pyvenv .venv
or
python3 -m venv .venv
There is an error:
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/user/.venv/bin/python3.5', '-Im', 'ensurepip', '--upgrade', '--default-pip']
I tried running both
sudo apt-get install python3-venv
and
sudo apt-get install python3.5-venv
but it did not solve my problem.
try installing python3.6-venv:
sudo apt-get install python3.6-venv
It seems that it was a locale problem. Solved by executing:
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales
found on this thread Python locale error: unsupported locale setting
Under Windows Linux Subsystem and Ubuntu 18.04, this was caused by my not having upgraded recently.
I ran:
sudo apt update
sudo apt upgrade
Then sudo apt install python3-venv worked.
Note that I had also tried the UTF-8 solution beforehand (I made it part of my .bashrc), so that could have been a contributing factor.
One of the other answers fixed it for me last time, but with Python 3.7 I had to do:
apt install python3-pip python3-setuptools python3.7-venv
Followed by
python3.7 -m venv /path/to/venv
Resolved similar problems on Ubuntu18 when came upon this answer. It is similar to the one that worked for #Niko Rikken, except it doesn't really need any new PPA's and "python3.8-distutils" package. I was installing new python3.8 environment with venv and I already had "python3-venv" installed and up to date, so my solution was to install only "python3.8-venv":
% sudo apt-get install python3.8-venv
And that got this lines working:
% python3.8 -m venv ~/envs/new_env
% source ~/envs/new_env/bin/activate
In my case the next steps worked:
Ubuntu 18.04.4 LTS
$ sudo apt-get install python3-venv python3.7-venv
$ python3.7 -m venv [your_path_to_virtual_env_here]
In case this helps anyone down the line, I was getting the same error on Ubuntu 18.04. Setting the locales didn't work and trying to install python3-venv gave the error:
$ sudo apt-get install python3-venv
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
python3-venv : Depends: python3.6-venv (>= 3.6.5-2~) but it is not going to be installed
Depends: python3 (= 3.6.5-3) but 3.6.7-1~18.04 is to be installed
E: Unable to correct problems, you have held broken packages.
And it looks like the apt repository had two versions of python:
$ apt list python3 -a
python3/bionic-updates,now 3.6.7-1~18.04 amd64 [installed]
python3/bionic 3.6.5-3 amd64
I tried to install Python3.6.5-3 but apt wanted to uninstall every dependency. I was able to solve the problem by installing Python3.7 and creating the venv with that:
$ sudo apt-get install python3.7 python3.7-venv
$ python3.7 -m venv my_venv
I encountered this problem on Ubuntu 18.04 for the recent release of Python-3.8. My solution was to add the Deadsnakes PPA which supplies the required python3.8-distutils package. The python3.8-venv package is already in the repository. Thanks to this blogpost:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8
sudo apt install python3.8-distutils
sudo apt install python3.8-venv
Note: This is only a temporary solution. In the near future the required python3.8-distutils package will probably be available in the default Ubuntu repository.
Edit:
For Ubuntu 20.04 LTS the python3-distutils package is based on Python 3.8. As of yet there is no Python 3.8 package distutils package available for Ubuntu 18.04 LTS.
As mentioned in other comments on this thread, distutils might not be required. It was for my use-case, but please consider that solution before adding additional PPA's.
First, make a directory :
mkdir testing
Then, moved to this directory named testing :
cd testing
When you type following command in this directory:
python3 -m venv env
You got error like :
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
Type the following command but before that keep an eye on the version of python you installed on the machine; in my case its python3.8
sudo apt install python3.8-venv
Now, we can create a virtual environment and store its tools in the "bhandari" folder .
python3 -m venv bhandari
Note: you can named this "bhandari" folder; anyname you like( Standard practice is to name it "env" ...)
Now to activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “bhandari” folder
source bhandari/bin/activate
If you have successfully activated your virtual environment, you should see the (bhandari) word indicating that we are working in a virtual environment.
After this, we can install anything that will be isolated from the rest of the system....
I was faced with the same problem and I am searching for a solution. It is about the problem:
ensurepip is disabled in Debian/Ubuntu for the system python.
And this my solution:
python3 -m venv myvenv --without-pip --system-site-packages
Try the following commands:
sudo apt install python-virtualenv
virtualenv --python=python3.6 myenv
These commands might work for you.
If you get any error like E: Unable to locate package python3-venv
Then try the following commands:
sudo apt install python3.6-venv
Python updated or Default python changed and venv already installed
the venv installed on your system is installed by your previous python version(let say python3.6). that's why venv is not working with current python version(lets say 3.8).
so first check your default python or python3 version,( suppose ur current version is python3.8).
reinstall virtual environment by mentioning current python version(3.8) as follow
sudo apt-get install python3.xx-venv >> replace xx with your current/default python version
if this helps you plz upvote, I'm new on this platform.
Try : python3.* -m venv myvenv -
And don't forget to replace * with your specific version of python
I had to mention the specific version of python and replace python3.10 with you version
$ sudo apt-get update -y && sudo apt-get upgrade -y
$ sudo apt-get install python3.10-venv
Creating a virtual environment
$ python3.10 -m venv --system-site-packages Project_Name
If your intention was to get python3.8 incl. pip and venv on Ubuntu 18.04:
sudo apt install python3.8 python3.8-venv python3-pip # there is no python3.8-pip package
python3.8 -m venv venv
source venv/bin/activate
python --version # -> python 3.8.0
pip --version # -> pip 9.0.1 from /home/user/venv/lib/python3.8/site-packages (python 3.8)
Try installing python3-distutils as well.
Altogether,
for python 3.8, the following worked for me.
$ apt-get install python3.8 python3.8-venv python3.8-distutils python3.8-dev
I just ran across this issue on several Debian/Ubuntu systems. Same error as above.
Findings
I tried to create a venv manually with:
python -m venv venvdir
This failed in the same way as others have mentioned. But it did create the shell of the venv. So I tried running ensurepip:
venvdir/bin/python -m ensurepip
Traceback (most recent call last):
File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.9/ensurepip/__main__.py", line 5, in <module>
sys.exit(ensurepip._main())
File "/usr/lib/python3.9/ensurepip/__init__.py", line 266, in _main
return _bootstrap(
File "/usr/lib/python3.9/ensurepip/__init__.py", line 166, in _bootstrap
copy_wheels(dependencies, venv_wheel_dir, sys.path)
File "/usr/lib/python3.9/ensurepip/__init__.py", line 144, in copy_wheels
assert len(wheel_names) == 1, wheel_names
AssertionError: ['/usr/share/python-wheels/pyparsing-2.2.0-py2.py3-none-any.whl', '/usr/s
hare/python-wheels/pyparsing-2.4.7-py2.py3-none-any.whl']
This seemed odd. A quick look into /usr/share/python-wheels/ revealed - indeed - multiple versions of the same wheel as the error suggests. I have no clue why ensurepip is also ensuring that it only finds one wheel file, go figure.
Solution
A check of dpkg -S /usr/share/python-wheels indicated that the python-pip-whl package is the source of those files. This appears to be true for both Ubuntu and Debian.
So, I did:
cd /usr/share
sudo mv python-wheels python-wheels.old
sudo apt reinstall python-pip-whl
And it worked - no more errors. There are no longer duplicates in /usr/share/python-wheels
Absolutely no clue how there were duplicates or why ensurepip is so sensitive to duplicates there... Probably a package upgrade gone wrong somewhere.
I had the same problem - the python env has 2 versions for 2.7 and 3.6.
All you need to do is:
Install the latest version of pip by installing pyenv installer
Make sure you follow the steps of installing pyenv found here
Good Luck!
I had the same problem for an existing project when executing python3 -m venv venv. I had just updated my Ubuntu and Python versions. After removing the already existing venv folder the issue was solved. (I have also tried the UTF-8 solution.)
My problem were related to permissions and ownership.
I was logged in with a different user as the owner of the current directory, which led to this error.
After reviewing and fixing all permissions I was able to install the venv regularl
This worked for me...
Firstly, I ran
sudo apt-get update
Then
sudo apt-get install -y python3-venv zip
Ran into the same issue recently. None of the solutions mentioned above worked for me. I eventually get it to working by installing pip3.
apt-get install python3-pip
# then run
python3.8 -m venv env
If you came across this issue while trying to run python -m build to build a python package, this means there probabaly is a syntax issue in your setup.cfg or setup.py file that causes an error creating the temporary venv required for installing dependencies.
Using pip wheels . will give you a less misleading error message.
For Linux, it is not installed by default you have to install venv
// at first check python version
python --version
// install
sudo apt update
sudo apt install python3.8-venv
sudo apt install python3.8-distutils
// create new env
python3 -m venv project-name
source project-name/bin/activate
All of these suggestions didn't help me.
$ apt list python3 -a
python3/now 3.6.7-1~18.04 amd64 [installed,local]
python3/bionic 3.6.5-3 amd64
So I did: sudo apt-get install python3/bionic
Now I have python 3.6.5 and apt-list showed a better list:
$ apt list python3 -a
python3/bionic 3.6.5-3 amd64
With sudo apt-get install python3-venv/bionic I could install pythno3-venv and everything worked.
In my case, running sudo apt-get install python3.8-venv succeeds but it shows the same error when running python3 -m venv .venv.
Finally, this command works out without changing locale.
python3.8 -c 'import venv; venv.create(".venv", with_pip=True)'
Here is my answer for Ubuntu 14.04. I was able to make venv and pip work with various Python versions. Details:
3.4: Ubuntu 14.04 has Python 3.4 (as package python3.4 etc.). It works:
$ sudo apt-get install python3.4 python3.4-dev python3.4-venv gcc libc6-dev
$ mkdir /tmp/try3.4
$ python3.4 -m venv /tmp/try3.4
$ . /tmp/try/bin/activate
(try3.4) $ pip install print-hello-world
...
(try3.4) $ print-hello-world
Hello World!
If python3.4-venv is removed from the apt-get install command above, then python3.4 -m venv displays the same error message as in the question. However, the error message mentions apt-get install python3-venv to solve it, but that doesn't work, there is no such package. (The correct package name is python3.4-venv.)
Please note that Python 3.4 is fairly old, and some Python packages available in PyPI (via pip) don't work with it.
3.5: It can be installed from the deadsnakes repository. It works:
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.5 python3.5-dev python3.5-venv gcc libc6-dev
$ mkdir /tmp/try3.5
$ python3.5 -m venv /tmp/try
$ . /tmp/try/bin/activate
(try3.5) $ pip install print-hello-world
...
(try3.5) $ print-hello-world
Hello World!
3.6: Ditto, it can be installed from the deadsnakes repository. It works:
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.6 python3.6-dev python3.6-venv gcc libc6-dev
$ mkdir /tmp/try3.6
$ python3.6 -m venv /tmp/try3.6
$ . /tmp/try3.6/bin/activate
(try3.6) $ pip install print-hello-world
...
(try3.6) $ print-hello-world
Hello World!
3.7: It doesn't work, because pip install fails with import _ssl, and python3.7 in the deadsnakes repo doesn't have that module, because Ubuntu 14.04 ships with on old version of OpenSSL which Python 3.7 doesn't support. See more details in this bug.
3.8--: No Ubuntu 14.04 package for these Python versions in the Ubuntu or deadsnakes repositories.
In my case, the command failed because I was still in a virtual env but did not notice it very quickly.
If it is the case, try calling deactivate.
I do not have sudo rights and I have to use python3.5:
Install virtualenv: pip3 virtualenv
Create virtualenv without pip: python3 -m venv --without-pip <path>
Downloaded the proper pip bootstrap: https://bootstrap.pypa.io/pip/3.5/get-pip.py
Run: <path>/bin/python3 get-pip.py
I did 'source <path>/bin/activate' and I got a working environment.
The solution for installing python3-venv is accurate since debian/ubuntu split the python distribution across multiple packages, so you do not actually have a full python install. If you really do not want to install this apt package, here is an alternative
python3 -m pip install virtualenv
virtualenv .venv
This will create fully functioning venv.

0MQ in virtualenv

I was able to install 0MQ in Ubuntu 12.04 by doing the followinng:
$ sudo apt-get install libzmq-dev
$ sudo apt-get install python-zmq
but when I went to use it in a virtualenv it could not find the module. What do I have to do in my virtualenv to see it
Once you make your virtualenv and activate it, use pip to install Python packages. They will install into your virtualenv.
Alternately, when you create your virtualenv, enable system-wide packages (with the --system-site-packages switch) within it so that system-installed packages will be visible in the virtualenv.

Categories

Resources