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

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?

Related

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

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

Python virtual environment not installing packages in correct directory (python)

I have set up a virtual environment correctly have activated it and when I do "which python" it tells me the correct directory, but when I do pip install in the venv it installs the package in the default directory of the mac and not in my venv. I have tried using pycharm and installing packages with that, but it happens the same thing.
Edit:
Following I will do a walkthru of my steps, first I did python3 -m venv /path/to/new/virtual/environment, then I did source env/bin/activate, then I did which python and I got the intended directory, afterwards I did pip3 install numpy and I saw the installation process, then I did pip list and numpy was not there, I checked the directory manually and it still was not there. I retried all the same thing with pycharm with the same results.
Follow these steps in order -
In your current working directory :
python3 -m venv venv
source venv/bin/activate
(venv) pip3 install library_name
To check if libraries were properly installed or not :
(venv) pip3 freeze
and check if your installed libraries is displayed on the terminal screen or not.
To deactivate the virtual environment :
(venv) deactivate
This is due to permission issues
Run this command to change permission of your virtual environment folder
sudo chmod -R 777 venv
After activating your virtual environment
source venv/bin/activate
Then try to install Django by either Python3 or Python2
pip3 install django or pip install django
Then check the list of installed packages by either Python3 or Python2
pip3 list or pip list
NOTE: If you run sudo pip3 install django or pip install in virtual environment that is not writeable by default it will be installed outside of virtual environment
If you run pip3 install django or pip install in virtual environment that is not writtable by default it will be installed outside of virtual environment you will get OS Persmission Error
I have used pip3 and pip because you can install packages by either Python3 or Python2, thing to remember if you created virtual environment by Python3, use pip3 and if you created by using Python2 use pip.

how to install python packages in "site-packages"?

I made Virt2 virtual environment.
using $ python -m venv Virt2.
I want to install my custom packages in "site-packages" directory. However packages are installed in "dist-packages" directory.
what should I do to install packages in my python virtual environment site-packages??
my python version 3.6.2 (in /usr/local/bin)
You use sudo and sudo switches user to root, i.e. you're completely outside of you virtual env. System pip3 outside of virtual env installs packages into a system directory which is dist-packages.
Run pip install inside you virtual env without sudo.

Setting up a virtural enviroment (venv) with no system site packages

I want to create a virtual environment using Enthought's Canopy distribution, but with no site packages.
following: https://support.enthought.com/entries/21802240-Use-venv-not-virtualenv-with-Canopy-Python
I set up the environment, leaving off the -s to not install the site packages:
$ venv path/to/virtual_environment_dir
$ source path/to/virtual_environment_dir/bin/activate
And this uses the correct python, but still uses the system easy_install and pip
(env) $ which python
path/to/virtual_environment_dir/bin/python
(env) $ which easy_install
/usr/bin/easy_install
(env) $ which pip
/usr/local/bin/pip
So if I try to install anything, it just installs it globally.
Is there any way to install pip into the virtual environment?
You have to install setuptools and pip manually into the environment. venv in Canopy is backported from the venv in Python 3, so unlike virtualenv, it has no special support for pre-installing these packages into the new environment. Just follow the standard installation instructions for setuptools and pip using the new environment's python executable.
This is from Robert Kern's reply, I just keep having to look up the command so I'll post it here.
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py -O - | python

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