Using Python Virtual Environments

Refer: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

Install on Ubuntu 18.04

sudo apt-get install python3-venv

Create a virtual environment

python3 -m venv env

Activating a virtual environment

source env/bin/activate

Confirm you are pointing to your virtual environment

which python

You should seem something like

.../env/bin/python

Leaving the virtual environment

deactivate

Installing packages, when inside virtual environment

python3 -m pip install requests

Installing specific versions

python3 -m pip install requests==2.18.4

Upgrading packages

pip can upgrade packages in-place using the --upgrade flag. For example, to install the latest version of requests and all of its dependencies:

python3 -m pip install --upgrade requests

Using requirements files

Instead of installing packages individually, pip allows you to declare all dependencies in a Requirements File. For example you could create a requirements.txt file containing:

requests==2.18.4
google-auth==1.1.0

Tell pip to install all the packages in this file using the -r flag:

python3 -m pip install -r requirements.txt

Freezing dependencies

Pip can export a list of all installed packages and their versions using the freeze command:

python3 -m pip freeze

Which will output a list of package specifiers such as:

cachetools==2.0.1
certifi==2017.7.27.1
chardet==3.0.4
google-auth==1.1.1
idna==2.6
pyasn1==0.3.6
pyasn1-modules==0.1.4
requests==2.18.4
rsa==3.4.2
six==1.11.0
urllib3==1.22

This is useful for creating Requirements Files that can re-create the exact versions of all packages installed in an environment.

Python Pathlib

Refer: https://stackabuse.com/introduction-to-the-python-pathlib-module/


import pathlib
current_dir = pathlib.Path.cwd()  
home_dir = pathlib.Path.home()  
print(current_dir)  
print(home_dir)

Though the code works, it looks clunky and is not readable nor easy to maintain. Imagine how this code would look if we wanted to create a new file inside multiple nested directories.

The same code can be re-written using Pathlib module, as follows:


import Path  
outpath = Path.cwd() / 'output' / 'output.xlsx'  

Write a Python Module / Package

Refer: https://stackoverflow.com/questions/15746675/how-to-write-a-python-module-package

Python 3 - UPDATED 18th November 2015

Found the accepted answer useful, yet wished to expand on several points for the benefit of others based on my own experiences.

Module: A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

Module Example: Assume we have a single python script in the current directory, here I am calling it mymodule.py

The file mymodule.py contains the following code:

def myfunc():
print("Hello!")
If we run the python3 interpreter from the current directory, we can import and run the function myfunc in the following different ways (you would typically just choose one of the following):

>>> import mymodule
>>> mymodule.myfunc()
Hello!
>>> from mymodule import myfunc
>>> myfunc()
Hello!
>>> from mymodule import *
>>> myfunc()
Hello!
Ok, so that was easy enough.

Now assume you have the need to put this module into its own dedicated folder to provide a module namespace, instead of just running it ad-hoc from the current working directory. This is where it is worth explaining the concept of a package.

Package: Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A. Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other’s module names.

Package Example: Let's now assume we have the following folder and files. Here, mymodule.py is identical to before, and __init__.py is an empty file:

.
└── mypackage
├── __init__.py
└── mymodule.py
The __init__.py files are required to make Python treat the directories as containing packages. For further information, please see the Modules documentation link provided later on.

Our current working directory is one level above the ordinary folder called mypackage

$ ls
mypackage
If we run the python3 interpreter now, we can import and run the module mymodule.py containing the required function myfunc in the following different ways (you would typically just choose one of the following):

>>> import mypackage
>>> from mypackage import mymodule
>>> mymodule.myfunc()
Hello!
>>> import mypackage.mymodule
>>> mypackage.mymodule.myfunc()
Hello!
>>> from mypackage import mymodule
>>> mymodule.myfunc()
Hello!
>>> from mypackage.mymodule import myfunc
>>> myfunc()
Hello!
>>> from mypackage.mymodule import *
>>> myfunc()
Hello!
Assuming Python 3, there is excellent documentation at: Modules

In terms of naming conventions for packages and modules, the general guidelines are given in PEP-0008 - please see Package and Module Names

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Pylint Tweaks Print Statement Error

Refer: https://stackoverflow.com/questions/48626676/vs-code-shows-an-error-message-at-print-statement-in-python-2-7
Refer: https://code.visualstudio.com/docs/python/linting

Create yourself a default file:

pylint --generate-rcfile > .pylintrc

Update the following section and add this entry

[MESSAGES CONTROL]
disable=print-statement

Visual Source Code

Edit /home/mruckman/.config/Code/User/settings.json and update this section for pylint:

    "python.linting.pylintArgs": [
        "--max-line-length=120",
        "--rcfile=/home/mruckman/Documents/Scripts/Python/pylintrc"
    ],

Download: pylintrc.txt

Accessing Oracle with Python

Ubuntu 14.04
$ sudo apt-get install python-dev
$ sudo pip install --upgrade pip
$ sudo -H pip install JayDeBeApi
Refer: https://pypi.org/project/JayDeBeApi/
Another option, untried: http://cx-oracle.readthedocs.io/en/latest/installation.html#quick-start-cx-oracle-installation