Changing Python Packages Path for Django Application - python

I'm currently hosting with a dreamhost shared VPS. I do not have sudo rights. I want to install django packages but I cannot to /usr/local/lib/python2.7/dist-packages/ because python is root access only. I have setup a directory in my user directory also called dist-packages to which I have copied all my packages to. I want to use the packages in the dist-packages directory I created to build my django app. Is possible to use my packages? I'm looking for a work around. I do have virtual python env installed, is there a way to use it maybe?
Python 2.7
Django 1.9
My current setup
/home/myuser/mydomain.com/
env/
myApp/
passenger_wsgi.py
public/
How do I setup the env?

You should be using virtualenv for sure(I'm surprised that you are still looking for a work around). It's really inconvenient to use default python for everything. virtualenv will create an isolated python directory of your choice and install everything in there. When you want to use it, just "activate" it then you are automagically in that python environment.
It's almost trivial to learn how to create one, you can even create as many as you like, each with different packages installed. Check their doc for more details.

Related

Installing Django and modules in virtual environment

My question is do i have to install django every single time in my virtual environment in order to run my python files? and is this taking up bunch of space on my machine? My project also uses "matplotlib" and every virtual environment i create it also asks me to import the matplotlib module too. its getting annoying. do i have to do this every time?
Im new to Django. I wanted to run some python files in django but they weren't working, so after some research i found out i needed to run my pycharm project in a virtual environment in order to run these python files.
my folders look like this pycharmProjects -> my project
I enter pycharmProjects and I set up virtual environment using "pienv shell". Then i run "python3 manage.py runserver". It turns out i must install django in the virtual environment before the files run.
Short answer is no, you don't have to use a virtual environment at all and can install your dependancies globally instead. However you will soon find that it will cause a lot of issues. The main reason you would create a virtual environment is to give control of your dependancies and prevent bugs that could be caused because of them having their wires crossed between projects.
Short answer yes.
If you create a virualenv you have to install all packages, that your program needs.
Long answer:
You could install django system wide and then create a virtualenv with the option
--system-site-packages then django would be used from your globally installed python.
(Or you install everything just in your global python, put I personally don't think this is good practice)
If you work with many different projects I think you will avoid a lot of trouble if you use one virtualenv per project.
Trouble meaning that one project breaks, because one pip install for another project changed the version of one package and one project can't handle the newer version.
I would recommend to create a requirements.txt file for each project, that lists the dependencies then you can create the virtualenv with following command
pip install -r requirements.txt
if you have requirement.txt files, then you can create virtualenvs rather quickly if going back to an old project and you can delete the virtualenvs whenever you run out of disk space. If you want to be an the safe side, type pip freeze > pipfreeze.txt prior to deleting the virtualenv and use pip install -r pipfreeze.txt if you want to create one with the same modules and the same versions.
You also might want to look at direnv or autoenv if working on a linux like system.
This will automatically switch to the required virtualenv when changing to a project's working directory.

Packaging and deploying a self-contained Python app

I have a bunch of Python scripts that I want to deploy to other machines. Thing is, I want to have everything self-contained and not depend on the other machines' libraries. For example I don't want to request users to have virtual environment and pip as installed in order for my app to work.
On my local machine I use virtual environment with --no-site-packages and pip install -r requirements.txt to get everything in place.
The bad news is virtualenvironment's activate script has my local path hardcoded into it and using the --relocatable option does not help with this situation so I suppose virtualenvironment is out of the question?
What I would like to have is something similar to this:
base_app_dir:
- main_app_dir
- my_init_script.py
- bin(includes python binary)
- lib(includes pip installed packages and python libraries)
so that I can instruct the end user to just cd into base_app_dir and do a ./bin/python -m my_init_script.py but that means I now need to instruct Python to look into my ./lib folder when importing packages.
I've tried setting os.path.insert(1, 'base_app_dir/lib/site-packages') but this work on per module basis.
Also how about lookup for default Python modules? Right now for example when import hashlib it tries to get it from /usr/lib/python2.7/hashlib.py. I would like to deploy these default Python modules as well and instruct the app to import them from my custom location.
Py2exe or creating a .deb file is not an option right now so please try to address my specific question.

Virtualenv, Django and PyCharm. File structure

I am newbie using VirtualEnv and recently try to create one using PyCharm. During the process, PyCharm ask me to specify the project location, application name and VirtualEnv name and location. My doubt is, after I specify the name and location of the VirtualEnv the location of the Django project files must be inside the VirtualEnv? or it's possible to have the VirtualEnv files in a different location than the Django project files?
Maybe I am not understanding the purpose of the VirtualEnv. Perhaps, VirtualEnv it's just a list of the dependencies of my project, Python version, Django version, Pip version, Jinja2 version and all other required files, but not necessarily the Django application files (the website that is being developed).
Thanks in advance.
Ya, I think you misunderstand what virtualenv does:
https://virtualenv.pypa.io/en/latest/
The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.
Your project files don't need to be (and shouldn't be) where the virtualenv files are.
virtualenv installs your app's python dependancies in a folder for the specific virtualenv that is being used.
Let's say you are not using virtualenv, the dependencies would be installed into into the site-packages folder for your system. The dependancies aren't installed in your project directory and your project directory isn't in your system's site-packages directory.
Using virtualenv doesn't change that, it just changes where the dependencies are installed.
virtualenv is not just a list of dependencies! It actually has all the modules under its umbrella. Think of a virtualenv as a space which isolates all the packages used by your project from the rest of the other packages that were installed previously or at a later time.Yes, there is an option to have the virtualenv make use of packages that are "outside" of the environment but that's just an option.
The main purpose of having an virtualenv is to enable the user to use packages versions of his choice and keep them isolated from the rest of the space. Usually, the list of packages belonging to a specific virtualenv are captured into a file, requirements.txt. If you want to run the project on a different machine or share it with someone, having requirements.txt will make it easy to recreate the environment via pip install -r requirement.txt from within virtualenv

Django and file permissions problems

I installed Django 1.5.4 on my MAC OSX 10.6.8. I created a test project.
And I am unable to edit any of those files, it says read only. I can sudo it and do the modifications.
But what are the other files it may try to access during run time, if they are read only then my application is not working, for example: sqlite database file "storage.db". Can I change the file permissions on the project root folder level and have it applied all the files inside?
Even If could do that, why Django starting project like this? I tried Virtualenv as well, but of no use same thing happens
I figured out the problem. Its the way I installed django in my system. Previously I used sudo easy_install , but this time I created a virtual env and installed django with pip and it worked well. Virtual environment is the best solution for package installation in python. It wont mess with system packages.

Installing modules into a custom site-packages directory

I have inherited a half-finished django project
which uses a custom site-packages directory that
is checked into subversion along with the code.
Being a newbie to python, I'm wondering if I can just
copy the contents of some eggs - that I want to add
into the django project - directly into this
site-packages directory from my ubuntu dist-packages directory.
/usr/local/lib/python2.6/dist-packages
Are there gotchas that I should watch out for if I do this manually ?
You need to use virtualenv
virtualenv is a tool to create
isolated Python environments.

Categories