Install site-packages inside virtual environment python - python

I am working on deploying my django application using uwsgi and nginx on a RHEL with pre installed python packages. Server is installed with uwsgi and nginx globally(as root). My server is not connected to internet.
I have secure copied my django project inside this server. For best practices I am told to use virtual environment and when I do the command,
virtualenv -p /usr/local/lib/python3.5/bin/python3.5 venv
All is cool. It creates a virtual environment with python 3.5.
But the problem is, I need these site packages which is pre installed on the server into this virtual environment. Example django, redis etc. When I do the above command with --system-site-packages it throws me this error
PermissionError: [Errno 13] Permission denied:
'/usr/local/lib/python3.6/site-packages/wheel-0.29.0.dist-info'
Is there a cleaner way to do this or where is it going wrong `?

You may try:
$ pip freeze
where pip is the system-wide pip. If it outputs anything, then just do:
$ pip freeze > requirements.txt;
$ source /path/to/venv/bin/activate && pip install -r requirements.txt;
In this way you will tell the virtual environment's pip to install all the python packages that are installed system-wide.
If pip freeze doesn't work, this is a privilleges issue, you have to elaborate this as/with an admin.
UPD: You may need to install additional packages as python3-dev, build-essential etc for pip install -r requirements.txt to work.

This might be happening because of the existence of another user in your machine.Just check if you have the system privileges , if not edit the properties of the file and grant it permission.

Related

Linux using Python 2 instead of 3

I recently installed linux minut on my laptop and am curretley trying to learn databases with django and python. My issue is that Linux seems to be using python 2 instead of python 3. So when pip installing django it installs the 1.11 version instead of the 2.0.
I think it has something to do with this error message
The directory '/home/zac/.cache/pip' or its parent directory is not
owned by the current user and caching wheels has been disabled. check
the permissions and owner of that directory. If executing pip with
sudo, you may want sudo's -H flag.
Django2 is only supported by python3, Django-1.11.x was the last version of Django supported by python2. To use django2 you need it to use using python3.
I strongly recommend using virtual environment for all django development.
You can follow this process:
Install pip3
sudo apt-get install python3-pip
Install Virtual Environment for Python3
sudo pip3 install virtualenv
Create a project directory
mkdir ~/newproject
cd ~/newproject
Create a new virtual environment and activate it
In this environment pip by default is pip3 and python by default is python3.
virtualenv .venv
source .venv/bin/activate
Now Install Django
pip install django
and then create project and start it,
django-admin startproject my_project
cd my_project
python manage.py runserver
It should work this way.

python: how to install and use setuptools in virtual python

I do not have root previlege on a linux server so I want to creat a virtual python according to creating a "virtual" python.
After I run virtual-python.py, I do have python in ~/bin/python:
Then, according to setuptools PyPI page, I download ez_setup.py and run ~/bin/python ez_setup.py. Error occurs:
What should I do?
Looking at the linked website, it looks outdated. You use pip, not easy_install.
For installing development packages, I always take the following rules in account:
The system package manager is responsible for system-wide packages, so never use sudo pip. This doesn't just match the question, but this is always a good idea.
The package manager packages are probably outdated. You'll want an up-to-date version for development tools.
I recommend the following way to install local development tools.
$ # Install pip and setuptools on a user level
$ curl https://bootstrap.pypa.io/get-pip.py | python - --user
$ # Add the executables to your path. Add this to your `.bashrc` or `.profile` as well
$ export PATH=$PATH/$HOME/.local/bin
At this point pip should be accessible from the command line and usable without sudo. Use this to install virtualenv, the most widely used tools to set up virtual environments.
$ pip install virtualenv --user
Now simply use virtualenv to set up an environment to run your application in:
$ virtualenv myapp
Now activate the virtual environment and do whatever you would like to do with it. Note that after activating the virtual environment, pip refers to pip installed inside of the virtualenv, not the one installed on a user level.
$ source myapp/bin/activate
(myapp)$ pip install -r requirements.txt # This is just an example
You'll want to create a new virtual environment for each application you run on the server, so the dependencies can't conflict.

python console error: ImportError: No module named 'django'

I am following the django quick install instructions.
I activated the venv
I did pip install django from inside the venv, with and without sudo and receive the same message.
To check the install:
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'django'
"did you...?"
No. I didn't. I didn't do anything extra. This is a new spawning of a vm (built by vagrant/puppet) that already has virtualenv installed. The only thing I had to do to prep this venv was to make it not use the systems python 2.7.8. virtualenv --python=/usr/bin/python3.4 myvirtualenv
(On a separate pyramid-based project, it would fail during setup (python setup.py development.ini would fail with invalid command name 'development.ini' I don't need that project for this project, but saying this here may reveal something about the install.).
So how do I make this go away?
Edit: clarified for answers from #catavaran & #Burhan Khalid
I suspect here is where you went wrong:
You created a virtual environment (virtualenv --python=/usr/bin/python3.4)
Next, you forgot to activate it; or you activated it and then you did sudo pip install django.
sudo pip will run the system wide pip, installing django for Python 2.7.8, rather than for the virtual environment.
If you exit the virtual environment and try to import django - it will work.
To fix the problem, make sure you activate the virtual environment and then do not use sudo to install packages.
Before installing/using django you should activate your virtualenv. And do not sudo while you call pip:
$ source ~/.virtualenvs/myenv/bin/activate
$ pip install django
Turns out the problem was permissions earlier in the setup.
It leads to Django being unable to complete it's install. pip install django would fail with a permissions error on a mkdir during the Cleaning up... segment of the install. And while all the files may have been installed, I suppose it wasn't completely configged.
sudo pip install django is no better because the django install now has su permissions which helps with the mkdir problem above, while potentially creating other problems.
After both installs, the python console import django would error.
The fix: the virtual env dirs need to be owned by the user that invoked the virtual envs.
sudo chown -R youruser:youruser /opt/myenv
In my case, puppet created the venv and dirs as the root user.

Python: PIL/_imaging.so: invalid ELF header

I'm using a virtualenv to run Python 2.7 in my local machine and everything works as expected. When I transfer "site-packages" to my production sever, I get the follow error:
PIL/_imaging.so: invalid ELF header
This happens on the Pillow 2.5.3 pypi package found here
I am running OS X, while my production server is running Debian. I suspect the OS differences might be causing the issue but I'm not sure. I have no idea how to fix this. Can anyone help?
Note: I cannot install packages directly to my production server, so I have to upload them directly to use them.
In your current virtual environment, execute the following command
pip freeze > requirements.txt
Copy this requirements.txt file to your server.
Create your new virtualenvironment (delete the one you were using before).
Activate the virtual environment and then type pip install -r requirements.txt
Now, the libraries will be installed correctly and built accurately as well.
If you see errors for PIL, execute the following commands:
sudo apt-get install build-essential python-dev
sudo apt-get build-dep python-imaging
virtual environments are for isolating Python on your current machine; they are not for creating portable environments. The benefit is to work with different versions of Python packages without modifying the system Python installation.
Using virtual environments does not require super-user permissions; so you can install packages even if you are not "root".
It does, however, require Internet access as packages are downloaded from the web. If your server does not have access to the Internet, back on your mac, do the following, from your virtual environment:
pip install basket
This will install basket which is a small utility that allows you to download packages but not install them. Great for keeping a local archive of packages that you can move to other machines.
Once its installed, follow these steps as listed in the documentation:
basket init
pip freeze > requirements.txt
awk -F'==' '{print $1}' requirements.txt | basket download
This will download all the packages from your requirements.txt file into ~/.basket
Next, copy this directory to your server and then run the following command from your virtualenvironment
pip install --no-index -f /path/to/basket -r requirements.txt

How to configure a virtual environment that doesn't require sudo?

In my Ubuntu 12.04 machine, the installation of pip requirements is asking me for sudo permission every time it attempts to install. How would I override this, as this is terrible for my working environment to install things globally instead of inside the venv?
Note: I did not setup the venv using sudo.
Have you activated your virtual environment? Run:
. bin/activate
in your shell. Then the local pip installation will take over the system one.
Thanks to #MartijnPieters, I found a workaround:
Running
~/.virtualenvs/myapp/bin/pip install -r requirements.txt
Instead of just
pip install -r requirements.txt
Make sure you use a recent version of virtualenv itself, the latest at the time of writing is 1.7.2. Old versions required the use of -E switch, to install into the virtual environment.

Categories

Resources