Category Archives: 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 https://docs.python.org/3/library/venv.html#module-venv

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 http://pythonsimple.noucleus.net/python-install/python-site-packages-what-they-are-and-where-to-put-them

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: https://dev.mysql.com/downloads/connector/python/ 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 http://pythonsimple.noucleus.net/python-install/python-site-packages-what-they-are-and-where-to-put-them

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 libaio.so.1 by running: sudo apt-get install libaio1 download instant client appropriate for your platform from oracle website: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html SDK and ODBC packages must be downloaded too. note: on my side i downloaded: oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm oracle-instantclient12.1-odbc-12.1.0.2.0-1.x86_64.rpm 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 http://stackoverflow.com/questions/16367504/python-parsing-html-elements-based-on-absolute-xpath http://lxml.de/api/

django – how to empty database [SOLVED]

To empty the database, simply run: python manage.py flush To force command line w/o having to confirm: python manage.py flush —noinput sources http://stackoverflow.com/questions/10605940/what-is-the-django-command-to-delete-all-tables https://github.com/django/django/blob/master/django/core/management/commands/flush.py

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

I am using ModelSerializers and faced hardtime ensuring foreign key could be optional: models.py: 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) serializers.py: 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 models.py, 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 mydomain.com 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 wsgi.py> Require all granted </Files> </Directory> WSGIScriptAlias / /path/to/myproject/myproject/wsgi.py WSGIDaemonProcess myproject python-path=/path/to/myproject:/path/to/myproject/virtualenv/lib/python3.4/site-packages WSGIProcessGroup myproject </VirtualHost> […]

Run python virtualenv with ubuntu

By default, my ubuntu runs python2.7 (i.e. simply run python –version). This comes from /usr/bin/python –> /usr/bin/python2.7 I changed the link to point to python3 instead. BIG MISTAKE Python is used all over the place by the system => apt-get update kept failing so I had to revert back to python2.7 link To use virtualenv with python3, things are pretty easy, you simply need to prefix your commands: sudo apt-get install python3-pip sudo pip3 install virtualenv To create a virtualenv: Go to folder where you want to create the virtual env: cd /path/to/my/project To create virtual env, run: virtualenv-3-4 myvirtualenv […]