How I can make apt-get install to my virtualenv? - python

It it's possible, of course.
For example - I can download python-dbus like this:
$ sudo apt-get download python-dbus
But what I should to do next, with this .deb package in my current virtualenv?

If you really need to do it this way, you can just copy the files that get installed globally directly into your virtualenv. For example I couldn't get pycurl working since the required libraries weren't installing, but apt-get install python-pycurl did. So I did the following:
sudo apt-get install python-pycurl
cp /usr/lib/python2.7/dist-packages/pycurl* ~/.virtualenvs/myenv/lib/python2.7/site-packages/
The install said it was adding it to /usr/lib/python2.7. So I looked in that directory for a site-packages or dist-packages with pycurl, after looking at the files I copied them into my virtualenv. You'd have to also copy any executables from bin into your virtualenv's bin directory.
Also, running a pip install -r requirements.txt successfully found pycurl in there and just skipped over it as if I had installed it via pip.

To include system site packages in your existing virtual environment open the config file:
<PATH_TO_YOUR_VENV_FOLDER>/pyvenv.cfg
and change false to true for include-system-site-packages
include-system-site-packages = true
Save and reload your virtual environment.
(tested with virtualenv 20.2.2 on Raspbian GNU/Linux 10 (buster) to pull in python3-pyqt5 installed with apt into my virtual environment)
If it is for a new environment #Joshua Kan's answer using the --system-site-packages flag with the venv command is probably what you want.

Why would you want to do this? The whole point is to avoid doing stuff like that...
virtualenv whatever
cd whatever
bin/pip install dbus-python
You may also choose to specify --no-site-packages to virtualenv to keep it extra isolated.

An alternative solution is to install globally, then followed by allowing the virtualenv to be able to see it.
As an example, let's say we want to install matplotlib for Python 3:
sudo apt update # Update first
sudo apt install python3-matplotlib # Install globally
sudo pip3 install -U virtualenv # Install virtualenv for Python 3 using pip3
virtualenv --system-site-packages -p python3 ./venv #the system-site-packages option allows venv to see all global packages including matplotlib
source ./venv/bin/activate #activate the venv to use matplotlib within the virtualenv
deactivate # don't exit until you're done using the virtualenv

First install the dbus development libraries (you may need some other dev libraries, but this is all I needed)
sudo apt-get install libdbus-1-dev libdbus-glib-1-dev
Next, with your virtualenv activated, run the following. It'll fail but that's ok.
pip install dbus-python
Finally, go into your virtualenv's build directory and install it the non-pythonic way.
cd $VIRTUAL_ENV/build/dbus-python
chmod +x configure
./configure --prefix=$VIRTUAL_ENV
make
make install

Related

Installing venv for python3 in WSL (Ubuntu)

I am trying to configure venv on Windows Subsystem for Linux with Ubuntu.
What I have tried:
1) Installing venv through pip (pip3, to be exact)
pip3 install venv
I get the following error
ERROR: Could not find a version that satisfies the requirement venv (from versions: none)
ERROR: No matching distribution found for venv
2) Installing venv through apt and apt-get
sudo apt install python3-venv
In this case the installation seems to complete, but when I try to create a virtual environment with python3 -m venv ./venv, I get an error, telling me to do apt-get install python3-venv (which I just did!)
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: ['/mnt/c/Users/Vicubso/.../code/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
I have also read the following posts
post 1,
post 2, and several others. None of these seem to solve my problem.
Any help would be much appreciated.
Nothing here worked for me, but this did in WSL2:
sudo apt-get update
sudo apt-get install libpython3-dev
sudo apt-get install python3-venv
python3.8 -m venv whatever
Good luck!
Give this approach a shot:
Install the pip:
sudo apt-get install python-pip
Install the virtual environment:
sudo pip install virtualenv
Store your virtual environments somewhere:
mkdir ~/.storevirtualenvs
Now you should be able to create a new virtualenv
virtualenv -p python3 yourVenv
To activate:
source yourVenv/bin/activate
To exit your new virtualenv, just deactivate
This was more of a headache than it needed to be. It seems that it relates to WSL<->Windows file system mapping issues. This blog post perhaps describes it better, but the net is you need to store additional metadata with files on a particular mount, as described in this MS devblog.
I fixed the issue by running:
sudo umount /mnt/c
sudo mount -t drvfs C: /mnt/c -o metadata
After which I was able to create python venv without needing to sudo.
The error occurs when you're in /mnt/XXX (under Windows part).
Switch to Linux part by cd and run python3 -m venv ./venv again and that should be fine
I was getting the same error message, I have WSL(Ubuntu) installed on my computer, finally I found this doc:
https://learn.microsoft.com/en-us/windows/python/web-frameworks#open-a-wsl---remote-window
Ironically the only difference from what I was using as command was the name, I was using venv, then I run the command again using .venv so that the files become hidden files instead, and it worked. Hopefully it'll help someone else :)
You need to install also python3.8-venv via
sudo apt install python3.8-venv
this fixed the problem for me.

installing and configuring virtualenv on ubuntu

I have installed virtualenv on my system using http://www.pythonforbeginners.com/basics/how-to-use-python-virtualenv
according to these guidelines, the initial step is:
$ sudo apt-get install python-pip python-dev python-virtualenv
However, I do not want to touch my parent environment. The only reason I believe virtualenv might be of some help for my case is because I have some weird errors that point to python version inconsistencies.
So my requirements are:
virtualenv with e.g. python 3.5
tensorflow
no influence on my parent environment
ability to disable virtualenv with no side effects
Is it doable how?
You could follow the steps in this answer for instance, which will be essentially the same as the guide you've mentioned.
virtualenv installs libraries and all in a subfolder of your main system, and directs python to only use those, so they don't interfere with your main installation.
If you really don't want to touch anything in your system, you could always run tensorflow in a docker container (see this answer for some tips). But even that will require some installation in the "parent" system.
create the env
virtualenv -p python3 path/to/your/env
activate the env
source path/to/your/env/bin/activate
install packages
pip install pkgname
deactivate
deactivate
If you do not want to touch your parent environment, install package using pip after activating the environment. Next time you activate the environment, the installed packages will remain there. If you want to delete the environment, just delete the folder path/to/your/env.
Just run this single command:
It installs python package manager: pip.
It creates a virtual environment named: my_env.
It activates the virtual environment.
sudo apt-get install python3-pip -y && sudo apt install python3.8-venv && python3 -m venv my_env/ && source my_env/bin/activate

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.

How to import a globally installed package to virtualenv folder

So I have a virtualenv folder called venv for my python project.
I can run:
venv/bin/pip install -r requirements.txt
Which installs all requirements I need for the project except one, M2Crypto. The only way to install it is through apt-get:
apt-get install python-m2crypto
How can I then add this package installed through apt to venv folder?
--system-site-packages
gives access to the global site-packages modules to the virtual environment.
you could do:
$ sudo apt-get install python-m2crypto
$ virtualenv env --system-site-packages
... and you would then have access to m2crypto (along with all other system-wide installed packages) inside your virtualenv.
What I did after all:
cp -R /usr/lib/python2.7/dist-packages/M2Crypto /home/richard/hello-project/venv/lib/python2.7/site-packages/
cp -R /usr/lib/python2.7/dist-packages/OpenSSL /home/richard/hello-project/venv/lib/python2.7/site-packages/
Real simple solution.
In the virtual environment directory, edit the file pyvenv.cfg. Set the parameter include-system-site-packages = true, and save the file.
The globally installed modules will appear the next time you activate (source venv/bin/activate) your environment.
It can be verified via pip list.
Enjoy!
toggleglobalsitepackages will toggle access to the system-wide site-packages.
Note: You need to pip install virtualenvwrapper to get this command; the vanilla virtualenv doesn't include it. With virtualenvwrapper you also get the very useful mkvirtualenv and rmvirtualenv commands, among others.
venv/bin/pip install -I M2Crypto
The -I forces it to also be installed into the virtualenv, even if it's already globally installed.
The only way to transfer packages locally from one environment or global to the other virtual environment is
Copying the the 📁"Lib"folder or the package folder with all its contents from the environment to the other environment you want the package to work
If you don't know it's location search for it within the environment folder using file explorer
Lib folder contains all the installed packages of the environment

How to install virtualenv without using sudo?

I have easy_install and pip.
I had many errors on my Linux Mint 12, I just re-installed it and I want to install everything from scratch again.
This is one of the errors that I had. I received an interesting answer there:
Stop using su and sudo to run virtualenv.
You need to run virtualenv as your normal user.
You have created the virtualenv with sudo which is why you are getting these errors.
So how to install virtualenv without using sudo? Can i use pipor easy_install without using sudo? Or is there another way?
This solution is suitable in cases where no virtualenv is available system wide and you can not become root to install virtualenv. When I set up a debian for python development or deployment I always apt-get install python-virtualenv. It is more convenient to have it around than to do the bootstrap pointed out below. But without root power it may be the the way to go:
There is a bootstrap mechanism that should get you going.
Read: http://peak.telecommunity.com/DevCenter/EasyInstall#creating-a-virtual-python
In essence you would do this in your home directory in a unix environment:
Given your python is version 2.6
$ mkdir ~/bin
$ mkdir -p ~/lib/python2.6
$ mkdir -p ~/local/lib/python2.6/dist-packages
$ wget http://peak.telecommunity.com/dist/virtual-python.py
$ python virtual-python.py --no-site-packages
$ wget http://peak.telecommunity.com/dist/ez_setup.py
$ ~/bin/python ez_setup.py
$ ~/local/bin/easy_install virtualenv
$ ~/local/bin/virtualenv --no-site-packages thereyouare
There may be room for optimization. I don't like the local path. Just bin and lib would be nice. But it does its job.
You can also use the command below, it worked for me without sudo access.
You may also need to modify your PYTHONPATH environment variable using export, see this SO answer for more details.
pip install --user virtualenv
The general idea is to install virtualenv itself globaly, i.e. sudo easy_install virtualenv or sudo pip install virtualenv, but then create the actual virtual environment ("run virtualenv") locally.
http://opensourcehacker.com/2012/09/16/recommended-way-for-sudo-free-installation-of-python-software-with-virtualenv/ suggests the following:
curl -L -o virtualenv.py https://raw.githubusercontent.com/pypa/virtualenv/master/virtualenv.py
python virtualenv.py vvv-venv
. vvv-venv/bin/activate
pip install vvv
It seems to work well. It lets me install https://github.com/miohtama/vvv with pip.
If you get:
Cannot find sdist setuptools-*.tar.gz
Cannot find sdist pip-*.tar.gz
Try --extra-search-dir after downloading the tarballs at https://github.com/pypa/virtualenv/tree/develop/virtualenv_support
This worked for me:
pip install --target=$HOME/virtualenv/ virtualenv
cd somewhere/
python $HOME/virtualenv/virtualenv.py env
. env/bin/activate
Now I can pip install whatever I want (except for everything that needs to compile stuff with gcc and has missing dependencies such as the python development libraries and Python.h).
Basically the idea is to install virtualenv (or any other python package) into ${HOME}/.local. This is the most appropriate location since it is included into python path by default (and not only Python).
That you do by pip3 install virtualenv --prefix=${HOME}/.local (you may need to expand ${HOME}).
Make sure that you have export PATH=${HOME}/.local/bin:${PATH} in your ~/.profile (you may need to source ~/.profile it if just added)
I've created a "portable" version of virtualenv.
wget https://bitbucket.org/techtonik/locally/raw/tip/06.get-virtualenv.py
python 06.get-virtualenv.py
It downloads virtualenv.py script with dependencies into .locally subdir and executes it from there. Once that's done, the script with .locally/ subdir can be copied anywhere.
I solved my problem installing virtualenv for each user.
python3 -m pip install --user virtualenv
You might want to consider using Anaconda. It's a full-fledged Python distribution, that lives in a folder in e.g. your home directory. No sudo is necessary at any point and you get most of the popular packages.
$ wget https://.../Anaconda2-2.5.0-Linux-x86_64.sh # check the website for the exact URL, it can change
$ bash Anaconda2-2.5.0-Linux-x86_64.sh
$ conda install virtualenv
The easiest way I have seen so far is to install Anaconda.
It may be an overkill for you. For me the centOS running on the remote server had only python2.6 installed. Anaconda by default installs everything locally + it is python2.7
curl -O https://repo.continuum.io/archive/Anaconda2-4.2.0-Linux-x86_64.sh
Then
bash Anaconda2-4.2.0-Linux-x86_64.sh
Boom. You have all the packages like numpy and pip installed.
Then if you want virtualenv, just type
pip install virtualenv
sudo virtualenv -p python myenv1
sudo su
source myenv1/bin/activate
pip install mypackage
this is will install inside virtual environment
The lack of sudo is a common situation in many shared remote server.
It turns out, there is a simpler, lightweight, more secure solution. Just download an official "portable" virtualenv from here: https://bootstrap.pypa.io/virtualenv.pyz
And that is it! You can now run python virtualenv.pyz --help to your heart's content.
Official document: https://virtualenv.pypa.io/en/latest/installation.html#via-zipapp

Categories

Resources