installing and configuring virtualenv on ubuntu - python

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

Related

Installing lxml without pip

Hello internet strangers,
I need to do XSD verification with lxml, but do not have sudo abilities on the machine I'm using - so pip is not an option. I'm on Fedora 27 and found the source code for lxml, which requires:
sudo apt-get install libxml2-dev libxslt-dev python-dev
But I cant use sudo on the machine I'm deploying to. Once I have those dependencies all I need to do is get the lxml source code through wget or github, and then run setup.py install, but I cant do that if I cant install the above dependencies.
Help?
Useful links:
https://gist.github.com/blite/868292
https://github.com/lxml/lxml
I'd look into creating a virtual environment and installing dependencies there. Here are the docs: https://docs.python.org/3.6/library/venv.html
Basically you create a virtual environment like this:
$ python -m venv my_venv_name
That creates a folder call my_venv_name with a few folders under it (do this somewhere in your home directory). The issue the command
$ source my_venv_name/bin/activate
That activates your environment. At this point you can pip install into your virtual environment to your hearts content. You just need to remember to activate the environment for each session that you want to use it in.

Pip not working inside Virtual Env but works outside perfectly

Hello Guys I am tying to follow the installation here https://github.com/systers/portal and trying to deploy the server inside a virtual environment on my machine.
After lots of errors I decided to install a fresh copy of Ubuntu 16.04 and start
After the installation here are the things that I have installed using the given commands
I checked my current python and python3 versions using python --version and python3--version respectively and they are Python 2.7.12 and Python 3.5.2 respectively.
Easy Install. $ sudo apt-get install python-setuptools python-dev build-essential
pip. $ sudo easy_install pip
virtualenv. $ sudo pip install --upgrade virtualenv.
python3-dev tools.$sudo apt-get install python3-dev
Now after that I created a virtual env and activated it using the following commands
$ virtualenv venv1 --python=/usr/bin/python3
$ source venv/bin/activate
But now when I run the third command
$ pip install -r requirements/dev.txt
or even do
$pip --version
I get the error
bash: /media/rohan/New Volume/portal/venv1/bin/pip: "/media/rohan/New: bad interpreter: No such file or directory
Also in /venv1/bin the files pip,pip3 ,pip3.5 are present
I tried sudo easy_install pip thinking that it will install pip in the virtual environment but it installs to /usr/local/bin
Also I tried by creating a virtual env using the code
$virtualenv venv --python=/usr/bin/python
But that also doesnt work and this time also same error comes and in /venv/bin pip pip2 pip2.7 are present
PLEASE HELP
The problem appears to be that the path to your virtualenv has a space in it that isn't being escaped somewhere it should be.
Note the error you receive:
/media/rohan/New: bad interpreter: No such file or directory
So with that space in the path, it is trying to run a program that doesn't exist (/media/rohan/New) on a file that doesn't exist (Volume/portal/venv1/bin/pip).
Renaming New Volume to something without spaces like new_volume and then recreating a virtualenv should resolve this.
I had renamed the folder of virtual environment so that I was getting this error.
Then I renamed the venv folder name to the path mentioned in the error (That is the one which I had named while creating venv.) then tried to use pip and it worked.
Might be you have already solved your issue, but this is for the future visitors.

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 I can make apt-get install to my virtualenv?

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

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