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

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.

Related

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.

Python installation directory

On command $which python3$ , the location says /opt/homebrew/bin/python3 on my Mac. Is this okay for python to be in other directory than /usr/local/ ?
Yes. It will work. I mean if you change the location of installation directory, mac os will recognize it and python3 instruction will work.
Yeah it's absolutely ok. But it's better to create a project wise virtual env so that you don't messed up installing so many third party libraries globally in your system which could break system tools and other projects.
Installing vitualenv:
python3 -m pip install --user virtualenv
Creating a virtual environment.
cd {{your project directory}}
python3 -m venv env
Activate the virtualenv:
source env/bin/activate
Now if you run which python you will see the python is from your newly created virtual env.

Python - where are Libraries stored in virtual environments?

I created a new VENV and pip installed awscli there.
I can see it installed with pip list --local.
But when I type aws - it does not recognize the command (even though I activated that VENV).
How to setup paths so that newly installed library in VENV can be recognized?
There are many ways to create a virtual environment. Let's say:
python3 -m venv my_venv
But to install some package in that particular environment, you have to activate it by:
For windows:
my_venv\Scripts\activate
For Linux or Mac:
source my_venv/bin/activate
Then if you install a package by using pip, it will be installed in that particular virtual environment. Otherwise, it will be installed in the local environment.

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

virtual environment from Mac to Linux

I recently made a django project using virtualenv on my mac. That mac broke, but I saved the files and now I want to work on my project using my linux computer. I am now having some difficulty running the virtual environment in Ubuntu.
Does it even make sense to try and use a virtual env made in Mac OS on Ubuntu?
Thanks
You can just recreate the virtual environment on Ubuntu. The virtual env will have the python binary which will be different on a different system.
Install pip
sudo apt-get install python3-pip
Install virtualenv using pip
sudo pip3 install virtualenv
Create a virtual environment
virtualenv venv
You can use any name instead of venv.
You can also use a Python interpreter of your choice
virtualenv -p <python2> <python3> <python3.7> venv
Activate your virtual environment
source venv/bin/activate
You only have to recreate virtualenv. That's all.
If you have a requirements.txt file then use pip to install the requirements:
pip install -r requirements.txt

Categories

Resources