How can I use the python-dateutil library in Google App Engine? I am using the webapp2 framework and need to know how to include the library and use it in my script.
EDIT2: Here's the recommended way to do this now: https://cloud.google.com/appengine/docs/python/tools/libraries27#vendoring
EDIT: Thanks to #TimHoffman, the correct (App Engine) way to do this is documented here. Ignore what I said below.
I've actually had to do this exact thing. First, I created a folder in my app project called 'lib' to hold any python libraries that aren't included with App Engine (for project organization). Then, I downloaded the dateutil python source and placed it in the new 'lib' folder. Finally, in your actual app code, before importing the desired libraries, you must add this line:
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
which just places the 'lib' folder in the python path so that python knows where the module actually is. Then simply:
import dateutil
Alternatively, you could just put the module code directly in your app folder, and python will automatically look in your program's folder for the module. Also, make sure it's included somewhere in your app.yaml so that it actually gets uploaded to google's servers.
The key, though, is that you must include the code for the module somewhere with your app.
Related
I have a standalone program written in Python. At one point I decided to organize this program into packages but the interpreter did not recognize the packages anymore. This seems to be a classic drawback in Python.
Python 'No module named' error; 'package' is not a package
I fixed this by extending the system path like following code block inside the base dunder __ init__.py file:
import sys
import os
dirname, filename = os.path.split(os.path.abspath(__file__))
sys.path.extend(['{}/example_package'.format(dirname)])
This setup works, so I'm happy. But this does not feel like the right way to organize an application. I can't find much information on the net about deploying python software.
My questions: Is this method of path inclusion accepted when the program will be deployed and released as a standalone application? Is this a practical working method if I would decide to convert the python code into binaries?
I have to deploy a python function to GCP. The libraries I want to use a library (USD by Pixar specifically) which needs I need to build myself. To make the library accessible I need to make changes to $PATH an $PYTHONPATH.
So the problem is that I have to include everything in one project to deploy the function and I don't know where to start.
I have tried appending to PYTHONPATH on run-time but it gives no module error. Also I have no idea how can I change PATH variable to be able to use executables inside usd/bin folder
import sys
sys.path.append('lib/python') # relative path.
This issue has been driving me insane for the past few days.
So basically, I'm trying to port over a Pure Python project to a proper PyCharm project. This is to basically improve code quality and project structure.
I wish it was as simple as basically creating a virtualenv to house everything, but it isn't. This project will eventually be developed simultaneously by multiple developers with Git as source control, and the default libraries will be modified. I presume this means that the libraries should ideally be tracked by Git in the end. Virtualenv shouldn't help here as far as I know because it's not portable between systems (or at least that's still being tested).
This project will also be, in the future, deployed to a Centos server.
So the only plan I can think of to successfully pull off this would be to simply bring in all of the libraries (which was done using pip install -t Libraries <ExampleLibrary>) into a single folder, with a __init__.py inside, and use them from other python files as a package within the Pycharm project.
Is this possible / recommended? I tried various methods to reference these libraries, but they all don't work during runtime. Somehow when the files in the library import something else from their own package, an ImportError is raised saying that there's no such module.
Will accept any other suggestions too.
Using Pycharm Community Edition.
EDIT: After having a good night's rest I think the crux of the issue is really just project organization. Before I ported it over to Pycharm the project worked as expected, but this had all of the python files in the root directory, and the libraries in a subfolder of the root, with every project file having the same boilerplate code:
import os, sys
absFilePath = os.path.dirname(os.path.abspath(__file__));
sys.path.insert(1, absFilePath + "/lib")
I was hoping that by using Pycharm to help me flesh out the packages, I could avoid having repeated boilerplate code.
Note: Not full solution.
The addition of the template code below forces the file containing the code to be in the same directory as the libs folder.
For Pycharm, all I had to do was mark the libs folder as a source folder. Even with the addition of the template code to the file, the modified libraries still work as expected.
For the Python Shell, this template code is still needed:
import os
import sys
absFilePath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(1, absFilePath + "/lib")
Sorry if it is a stupid question.
Normally when I need a package, I import filename.py.
How is about this https://github.com/simplegeo/python-oauth2 ? How can I import this package since I cannot find oauth2.py file.
Do I copy this oauth2 folder to project root and then do import oauth2?
I am just a beginner so can you give me detail instruction?
Thanks for your time
Read this first.
Now if you check out oauth2/__init__.py, you will see oauth already do import httplib2 so you don't have to import it yourself. (Unless you also are using httplib2, of course.)
In essence, you are correct. If you want to use a python package in a GAE app you have to have a copy of it it in the applications root directory where it can be accessed by your application as a normal import. It will then be uploaded along with your applications code and be usable when deployed too.
There are exceptions where you don't have to do this as the library is provided for you, you can read about those here: Supported 3rd Party Libraries
For those you need to edit your app.yaml.
For oauth2 you should look at google specific implementations. I don't know what you've linked to there, all I know is the endless stream of oauth/gae questions usually end up here: https://developers.google.com/appengine/docs/python/oauth/overview
Im trying to add some libraries within my app to make it more portable.
The problem is that the app is built with not this in mind.
So to be more specific I want to have a folder in my package called libs and keep a copy of the libs used in my app there.
For example lets say I use MySQLdb library. In my app I am importing it like this
import mysqldb
I want to be able to use the same code but having mysqldb inside libs folder.
Is that possible? I tried some things with __init__.py but I failed.
You shouldn't be putting MySQLdb anywhere inside your project. It's a third-party library, it should either be installed as a system package or inside your project's virtualenv.
You can just play around with sys.path and should achieve what you want. Just do at the beginning of your program:
import sys
sys.path = [$'path_to_your_lib_folder_here'$] + sys.path
This is not a best practice however and you need to be carefull especially if your software will be used as a third-party by other libraries.