Deploy a Python Cloud Function with all package dependencies - python

I would like to deploy a cloud function that doesn't rely on using a requirements.txt to install packages. I want the packages to be available within storage or zipped up and upload as part of the function. Is this possible?
EDIT 6/14/2019
Basically I would like to send packages like numpy and pandas with my code to deploy a cloud function. I want to do this in the event that pypi.org is not available. I have tried following this piece of documentation. An example of what I am trying to do is below:
Folder Structure:
-> my_folder
-> main.py
-> libs
-> numpy (the entire package)
-> pandas (the entire package)
-> __init__.py
main.py
import libs.numpy as np
import libs.pandas as pd
def function()
do stuff with numpy and pandas
I then tried to deploy the function from gcloud command line and then gcp UI, both failed. If this is possible please help.

At the moment there is only two options:
Using the requirements.txt
Packaging the dependencies along with your function, link here
They cannot be zipped neither on storage, they will be treated as part of the source of the function.
If you choose to go with the second option, the parameter -t libs might help you.
You can use it to install everything on a libs folder and then you can just move the content to the local directory. As a single command it would look like this:
pip install -t libs [your library name(s)] && rm -rf libs/*.dist-info && mv -r libs/* . && rm -rf libs
I added the rm -rf libs/*.dist-info portion in order to not pollute the source folder with tons of library version and distribution information that are useless to the function. Those are used by pip when freezing and planning updates.
EDIT 6/14/2019
You kept the libraries on the libs folder. That is the point before the mv -r libs/* . on the one-liner that I added above.
Using a libs folder keeps everything more organized, so if you want to keep the packages there you need to vendor that folder adding this to the top of your main.py, before all other imports:
# Vendoring packages from libs folder
import sys
import os
sys.path.insert(1, os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"libs"
))
# All other imports go below this line
Explaining:
__file__ is a global variable present in every module that holds the path to the file from which the module was defined, that is the file where it is being used. In our case, the path to main.py.
Since we cannot be certain of the working directory at the moment main.py is imported we pass that to os.path.realpath to be certain of the path structure. Could be os.path.abspath to, I have seen and used both and haven't noticed any difference.
From the path of the file, we get the path of the directory of your source code with os.path.dirname and then to the libs folder inside it with os.path.join.
Now the most important part. When you try to import a package, python looks for them on the system/python path. So we add the libs full path that we built as the first lookup location on the system path after your working directory. New import statements will look on that folder first and the package is not there proceed normally with the rest of the lookup directories.
If you prefer to look for packages on libs only if they are not available in the system and the python environment, append the libs path instead of inserting it at index 1.
After that you don't need to prepend libs. on your imports, just use the normal import numpy.
On fully independent packages this might work, but not on packages with dependencies, since they expect their dependencies to be directly importable (from anywhere on sys.path).

Related

Python git repo setup not matching site packages

My team uses git to manage our workflow, however, I clearly don't have it set up right.
I have my repo setup in my user directory: Repos/project/stuff
However, the actual project and it's site packages are in a different location in the root directory: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/project/stuff
The specific issue I'm having is: I create a local branch from master, and make a change to a sample.py file which contains dictionaries of hard-coded data (a config file). Then I import sample.py into test.py and print its contents. However, the print contents are not reflective of the local branch where I've made changes, nor are they reflective of the master branch. The import is pulling from the site-packages folder I listed above, which I guess is outdated.
How would I get the import to happen from my local branch? Do I need to place my Repos folder in a different directory? Or is this a path issue?
Thanks in advance.
Python is importing from the site-packages folder because it's how Python's import works: site-packages is added to sys.path.
To make Python to import your updated code you have two ways:
Install the updated code after an every update. pip instal . in the git repository or python setup.py install.
Install the code once in "develop mode" using command pip instal -e . or python setup.py develop. After that Python will always import from the git repo. See https://setuptools.readthedocs.io/en/latest/userguide/development_mode.html

How to import a file from the folder that my .py script is being called from?

I'm currently working on a python package that I want to upload to pypi.
In the future, other devs will install this package via pip install and then load it into their .py files using import name_of_package.
In order to function properly, my python package needs to import another .py file from the same folder that it was imported into.
So let's assume someone works in /documents/python/some_project/ and he imports my package into his main.py script. My package script shall then search for a file called secondary.py in the same folder where main.py is located, namely /documents/python/some_project/ and then import secondary.py into the package script.
How can I write this import statement in my package script?
The classic approach is to use relative imports;
from .other_module import foo_function
from .other_module import BarClass
See The import statement in the Python doc.
You can also take a look at importlib.import_module.
But, in your specific case, you want to call an external Python script located potentially anywhere in the user directory.
For that, you can use exec function. But, make sur the code your are calling is safe code.
When you use pip install, the installed package is stored in the virtualenv of the user; not in the user’s source directory. I think you are also confused by that.

Importing files from different folder, *project-wide*

There is a popular Python question called Importing files from different folder.
But the top answer there mentions adding stuff to "some_file.py", and that will obviously only apply to imports inside that file.
What if I want to specify an additional dir to import from, project-wide?
I don't want to modify PYTHONPATH, as I believe a per-project solution is cleaner.
I don't want to use python packages for this, because I feel they'll probably just complicate stuff. E.g. maybe I'll need to manually recompile .py files into .pyc files every time I make a change to the code in the other folder.
What if I want to specify an additional dir to import from, project-wide?
Solution 1: Package installed in develop mode
Create a regular package with setup.py and install it with -e option:
python -m pip install -e /path/to/dir_with_setup_py/
-e, --editable Install a project in editable mode (i.e. setuptools "develop mode") from a local project
path or a VCS url.
Now, as soon as you update your code, the new version will be used at import
without reinstalling anything.
Solution 2: Dynamically modify sys.path
You can add as many directories dynamically to the search path for Python packages as you want. Make this the very first lines of code you execute:
import sys
sys.path.append('my/path/to/my/file1')
sys.path.append('my/path/to/my/file2')
or to make the first to be found:
sys.path.insert(0, 'my/path/to/my/file1')
sys.path.insert(0, 'my/path/to/my/file2')
Now the files:
my/path/to/my/file1/myscript1.py
my/path/to/my/file2/myscript2.py
can be imported anywhere in your project:
import myscript1
import myscript2
No need to modify sys.path again as long as this Python process is running.

how to set different PYTHONPATH variables for python3 and python2 respectively

I want to add a specific library path only to python2. After adding export PYTHONPATH="/path/to/lib/" to my .bashrc, however, executing python3 gets the error: Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python 3.x!
I think it is due to that python2 and python3 share the common PYTHONPATH variable.
So, can I set different PYTHONPATH variables respectively for python2 and python3. If not, how can I add a library path exclusively to a particular version of python?
PYTHONPATH is somewhat of a hack as far as package management is concerned. A "pretty" solution would be to package your library and install it.
This could sound more tricky than it is, so let me show you how it works.
Let us assume your "package" has a single file named wow.py and you keep it in /home/user/mylib/wow.py.
Create the file /home/user/mylib/setup.py with the following content:
from setuptools import setup
setup(name="WowPackage",
packages=["."],
)
That's it, now you can "properly install" your package into the Python distribution of your choice without the need to bother about PYTHONPATH. As far as "proper installation" is concerned, you have at least three options:
"Really proper". Will copy your code to your python site-packages directory:
$ python setup.py install
"Development". Will only add a link from the python site-packages to /home/user/mylib. This means that changes to code in your directory will have effect.
$ python setup.py develop
"User". If you do not want to write to the system directories, you can install the package (either "properly" or "in development mode") to /home/user/.local directory, where Python will also find them on its own. For that, just add --user to the command.
$ python setup.py install --user
$ python setup.py develop --user
To remove a package installed in development mode, do
$ python setup.py develop -u
or
$ python setup.py develop -u --user
To remove a package installed "properly", do
$ pip uninstall WowPackage
If your package is more interesting than a single file (e.g. you have subdirectories and such), just list those in the packages parameter of the setup function (you will need to list everything recursively, hence you'll use a helper function for larger libraries). Once you get a hang of it, make sure to read a more detailed manual as well.
In the end, go and contribute your package to PyPI -- it is as simple as calling python setup.py sdist register upload (you'll need a PyPI username, though).
You can create a configuration file mymodule.pth under lib/site-packages (on Windows) or lib/pythonX.Y/site-packages (on Unix and Macintosh), then add one line containing the directory to add to python path.
From docs.python2 and docs.python3:
A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.
I found that there is no way to modify PYTHONPATH that is only for python2 or only for python3. I had to use a .pth file.
What I had to do was:
make sure directory is created in my home: $HOME/.local/lib/python${MAJOR_VERSION}.${MINOR_VERSION}/site-packages
create a .pth file in that directory
test that your .pth file is work
done
For more info on `.pth. file syntax and how they work please see: python2 docs and python3 docs.
(.pth files in a nutshell: when your python interpreter starts it will look in certain directories and see the .pth file, open those files, parse the files, and add those directories to your sys.path (i.e. the same behavior as PYTHONPATH) and make any python modules located on those directories available for normal importing.)
If you don't want to bother with moving/adding documents in lib/site-packages, try adding two lines of code in the python2.7 script you would like to run (below.)
import sys
sys.path = [p for p in sys.path if p.startswith(r'C:\Python27')]
This way, PYTHONPATH will be updated (ignore all python3.x packages) every time you run your code.

Python: using downloaded modules

I am new to Python and mostly used my own code. But so now I downloaded a package that I need for some problem I have.
Example structure:
root\
externals\
__init__.py
cowfactory\
__init__.py
cow.py
milk.py
kittens.py
Now the cowfactory's __init__.py does from cowfactory import cow. This gives an import error.
I could fix it and change the import statement to from externals.cowfactory import cow but something tells me that there is an easier way since it's not very practical.
An other fix could be to put the cowfactory package in the root of my project but that's not very tidy either.
I think I have to do something with the __init__.py file in the externals directory but I am not sure what.
Inside the cowfactory package, relative imports should be used such as from . import cow. The __init__.py file in externals is not necessary. Assuming that your project lies in root\ and cowfactory is the external package you downloaded, you can do it in two different ways:
Install the external module
External Python packages usually come with a file "setup.py" that allows you to install it. On Windows, it would be the command "setup.py bdist_wininst" and you get a EXE installer in the "dist" directory (if it builds correctly). Use that installer and the package will be installed in the Python installation directory. Afterwards, you can simply do an import cowfactory just like you would do import os.
If you have pip or easy_install installed: Many external packages can be installed with them (pip even allows easy uninstallation).
Use PYTHONPATH for development
If you want to keep all dependencies together in your project directory, then keep all external packages in the externals\ folder and add the folder to the PYTHONPATH. If you're using the command line, you can create a batch file containing something like
set PYTHONPATH=%PYTHONPATH%:externals
yourprogram.py
I'm actually doing something similar, but using PyDev+Eclipse. There, you can change the "Run configurations" to include the environment variable PYTHONPATH with the value "externals". After the environment variable is set, you can simply import cowfactory in your own modules. Note how that is better than from external import cowfactory because in the latter case, it wouldn't work anymore once you install your project (or you'd have to install all external dependencies as a package called "external" which is a bad idea).
Same solutions of course apply to Linux, as well, but with different commands.
generally, you would use easy_install our pip to install it for you in the appropriate directory. There is a site-packages directory on windows where you can put the package if you can't use easy_install for some reason. On ubuntu, it's /usr/lib/pythonX.Y/dist-packages. Google for your particular system. Or you can put it anywhere on your PYTHONPATH environment variable.
As a general rule, it's good to not put third party libs in your programs directory structure (although there are differing opinions on this vis a vis source control). This keeps your directory structure as minimalist as possible.
The easiest way is to use the enviroment variable $PYTHONPATH. You set it before running your scripts as follows:
export $PYTHONPATH=$PYTHONPATH:/root/externals/
You can add as many folders as you want (provided their separate by :) and python will look in all those folders when importing.

Categories