Category Archives: python

flask + apache + mod_wsgi – not working [solved]

Gosh! it took me a whole day to identify what the issue was…. Basically what is happening: when using WSGIScriptAlias directive on non-root url (i..e you’re not matching “/”), mod_wsgi strip the url of the matched part Here is an example: in htttpd.conf: WSGIDaemonProcess credmanager user=myuser group=mygroup threads=5 python-home=/path/to/virtualenv WSGIScriptAlias /api /var/www/credmanager/api/credmanager.wsgi WSGIProcessGroup credmanager WSGIApplicationGroup %{GLOBAL} WSGIPassAuthorization On <Directory /var/www/credmanager> Options FollowSymLinks Require all granted </Directory> In above example, we are only handling via python uris starting with “/api”. Let’s say requested url: => url is starting with “/api” so it is matched and handled by mod_wsgi => python […]

creating a python virtual environment

When starting a new python project, it is usually sound practice to use a dedicated virtual environment. By doing so your project is isolated from other applications using python and all dependencies can be properly tracked. ensure python is installed ensure pip is installed (note: starting python3.4, pip is installed by default) ensure virtualenv is installed (note: starting python3.3, venv is installed by default) In a nutshell, if you have python >= python3.4, nothing to install!! Easy 🙂 Run below commands: cd <path/to/my/project> python3 -m venv ./virtualenv Done. Run source ./virtualenv/bin/activate to activate your virtual environment sources

python – which site-packages folder are used in my environment [solved]

Log into your python environment and run: import site print(‘\n’.join(site.getsitepackages())) print(‘\n’.join(site.getusersitepackages())) sources

python 3.5 – install oracle mysql driver on ubuntu [solved]

Installing this mysql driver is surprisingly quite a painful experience. Steps Download package relevant for your distribution and python version from oracle website: Install downloaded package: sudo dpkg -i mysql-connector-python-py3 _2.1.3-1ubuntu15.04_all.deb Go into python and run “import mysql” For all virtualenvs, simply symlink: ln -s /usr/lib/python3/dist-packages/mysql <path_to_virtual_env>/lib/pythonX.X/site-packages It should be good by now.

python – which site-packages folder are used in my environment [solved]

Log into your python environment and run: import site print(‘\n’.join(site.getsitepackages())) print(‘\n’.join(site.getusersitepackages())) sources

how to set up cx_Oracle on ubuntu [solved]

I tried installing oracle client to use it with sqlalchemy and ended up with a nasty error message: distutils.errors.DistutilsSetupError: cannot locate Oracle include files To setup cx_Oracle so that it can be imported from python, proceed as follow: install by running: sudo apt-get install libaio1 download instant client appropriate for your platform from oracle website: SDK and ODBC packages must be downloaded too. note: on my side i downloaded: oracle-instantclient12.1-basic- oracle-instantclient12.1-devel- oracle-instantclient12.1-odbc- convert into .deb package: sudo alien -d *instantcl*.rpm note: you may need to install alien (sudo apt-get install alien) if not already done install created package: […]

python lxml – use absolute xpath to find elements [solved]

When using lxml library, using an absolute xpath such as ‘/root/child/other_child’ end up with a nasty error message: SyntaxError: cannot use absolute path on element Reason for that is that call was performed on an Element using .findall() method. To use an absolute xpath, do not perform call on an Element, rather perform call on an ElementTree and call .xpath() method instead of .findall() Illustration below: etree.parse(fpath).getroot().findall(‘/root/child/’) # => SyntaxError: cannot use absolute path on element etree.parse(fpath).xpath(‘/root/child/’) # => OK sources

django – how to empty database [SOLVED]

To empty the database, simply run: python flush To force command line w/o having to confirm: python flush —noinput sources

django rest framework – ensure foreignKey field is optional [SOLVED]

I am using ModelSerializers and faced hardtime ensuring foreign key could be optional: class Tag(models.Model): label = models.CharField(max_length=100) group = models.ForeignKey(TagGroup, null=True, blank=True, related_name=’tags’ class TagGroup(models.Model): label = models.CharField(max_length=100) class TagSerializer(serializers.ModelSerializer): class Meta: model = Tag fields = (‘id’, ‘label’, ‘group’) group = TagGroupSerializer() class TagGroupSerializer(serializers.ModelSerializer): class Meta: model = TagGroup fields = (‘id’, ‘label’,) With above setup, though foreignKey field is clearly defined as nullable and not-mandatory in, i kept getting following error: ‘{“group”:[“This field may not be null.”]}’ Reason for this is that you also need to specify field as nullable and non-mandatory in the […]

Configure apache to run a django framework based project [solved]

To serve django via apache, using mod_wsgi (recommended option by django documentation), proceed as follow: install apache mod_wsgi: sudo apt-get install libapache2-mod-wsgi-py3 ensure mod_wsgi is indeed into /etc/apache2/mods-enabled: ls -l  /etc/apache2/mods-enabled/ | grep “wsgi” create a python virtualenv for your project (see Run python virtualenv with Ubuntu) go to /etc/apache2/sites-available and create file myproject.conf: sudo vi /etc/apache2/sites-available/myproject.conf Paste following content: <VirtualHost *:80> ServerName ServerAdmin webmaster@localhost DocumentRoot /path/to/myproject ErrorLog ${APACHE_LOG_DIR}/myproject.error.log CustomLog ${APACHE_LOG_DIR}/myproject.access.log combined Alias /static/ /path/to/myproject/static/ <Directory /path/to/myproject/static> Require all granted </Directory> <Directory /path/to/myproject/myproject> <Files> Require all granted </Files> </Directory> WSGIScriptAlias / /path/to/myproject/myproject/ WSGIDaemonProcess myproject python-path=/path/to/myproject:/path/to/myproject/virtualenv/lib/python3.4/site-packages WSGIProcessGroup myproject </VirtualHost> […]