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
Related
I have a project and I have to use gitdb with python. When I search on google I just find gitdb Documents. And it doesn't have any basic example. I found just that code snipped which I don't understand about it clearly.
#/usr/bin/python
import os,sys,zlib,gitdb
from gitdb.db import LooseObjectDB
from gitdb.pack import PackEntity
from gitdb.util import bin_to_hex,hex_to_bin
ldb=LooseObjectDB(sys.argv[1]+'/.git/objects')
PackEntity.create((ldb.stream(sha) for sha in ldb.sha_iter()),sys.argv[1]+'/.git/objects/pack',object_count=ldb.size(),zlib_compression=zlib.Z_BEST_COMPRESSION)
Could anybody have any recommendation for me?
Are you sure you have to to use gitdb? The gitdb module is a low-level module for interacting with the Git repository structure. It is used by GitPython, which is a higher-level interface to Git repositories.
I imagine you can either use GitPython -- which is well documented with lots of examples -- in your project, or you can at least use it as a reference to see how one uses the gitdb module.
I'm writing a Python script that will be used by multiple people on different OS's and I'm trying to figure out the best way to include a third-party package so that users don't have to install the package themselves before running the script. The particular library I'm using is the Requests package. It works great for me when I install it via pip, but when I download the source and try to import it from my project root, it doesn't work. I either get "Attempted relative import in non-package" or "No module named MyProject.request" depending on how I try to import it.
So I suppose my first question is, am I even approaching this right? I've never written a python script for more users than just myself, so I've never been concerned with issues like package installation. Is the correct approach to include the source for the Requests library and import it relative to my project, or is there an easier approach?
As a follow-up question, if I am on the right track, what's the proper procedure for including this package in my project? Right now my file structure looks like this:
MyProject/
__init__.py
main.py
requests/
requests/
__init__.py
[bunch of other stuff]
[other files/folders]
In main.py I've tried from .MyProject.requests import requests and from .requests import requests, and both give me "Attempted relative import in non-package." I haven't messed around with sys.path yet, but I've seen mixed advice as to whether that's the "right" way to do it. My primary concern is getting this script to run on user machines without them having to do anything besides run it. I don't want users to have to install an external package, but I can't seem to get it working by trying to import the source.
Per Evert's response, my problems seems to have been the multiple "requests" directories. When I moved the sub-directory up and eliminated the superfluous files/directories from the Requests source it worked perfectly.
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.
In my foray into learning more about Python, I've been studying the structure of a Python module called requests or sometimes known as python-requests.
Now, the question is this, which is a link to the api.py file in the requests github repo, you will see that it provides the interfacing for the module, which is to be expected. My question is how does Python know that all the interfacing is done in api.py, and this, is it mandetory to include an api.py file when making a reusable module?
I would be grateful for any links to documentation.
See here: https://github.com/kennethreitz/requests/blob/master/requests/__init__.py
E.g. if 'requests' is a directory, which has __init__.py, Python executes this file each time it sees from requests import ... or import requests.
See more in Modues.
For my project I would be using the argparse library. My question is, how do I distribute it with my project. I am asking this because of the technicalities and legalities involved.
Do I just:
Put the argparse.py file along with
my project. That is, in the tar file for my project.
Create a package for it for my
distro?
Tell the user to install it himself?
What's your target Python version? It appears that argparse is included from version 2.7.
If you're building a small library with minimal dependencies, I would consider removing the dependency on an external module and only use facilities offered by the standard Python library. You can access command line parameters with sys.argv and parse them yourself, it's usually not that hard to do. Your users will definitely appreciate not having to install yet another third party module just to use your code.
It would be best for the user to install it so that only one copy is present on the system and so that it can be updated if there are any issues, but including it with your project is a viable option if you abide by all requirements specified in the license.
Try to import it from the public location, and if that fails then resort to using the included module.
You could go with Ignacio's suggestion.
But... For what it is worth, there's another library for argument parsing built into Python, which is quite powerful. Have you tried optparse? It belongs to the base Python distribution and has been there for a while...
Good luck!