Uploading Python third party libraries - python

Google App engine documentation states that it is possible to upload and use third party libraries provided they written in pure Python.
What are the steps I need to take to do this?

What I did is created a file called fix_path.py in my root directory that looks like this:
import os
import sys
import jinja2
# path to lib direcotory
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib'))
Then I created a lib directory, and drop the module in there.
For example, I use WTForms. My file structure looks like this.
lib
wtforms
fix_path.py
somefile.py
when I am ready to call it from my somefile script
import fix_path # has to be first.
import wtforms
here is this example in my github source. checkout fix_path.py for setup and views.py for usage.

Well I tried the same with following steps.
created a directory(lib) with init file i.e lib/__init__.py in my project root.
created my module (mymodule.py), and defined a function i.e.
def myfunc():
return "mycustomfunction"
imported mymodule in my main.py
from lib import mymodule
I could use the returned value from myfunc() and could pass that as a template value to my jinja2 template
Similarly, if we follow what #rjz also pointed out in the first answer, if the 3rd Party library is just a module then we can keep that in libs with an init file and it can be imported with an import statement ( point 3) . If the 3rd party library is a package then we can keep it in the project root and import it again with an import statement as this one in the main.py:
from thirdpartypackage import *

Related

import module in Django

I have a project with structure like on this picture.
Folders structure
Where 'backend' folder is Django project folder.
I need to import module from another folder 'main' inside Django app file, i.e. import main.Text_Generator in backend.app.views file.
I tried: from ...main.Text_Generator import *. This raise an error while running a server: "attempted relative import beyond top-level package"
And from main.Text_Generator import *, also error "No module named 'main'"
What is the correct way to do such import?
Add this:
import sys
sys.path.append("..")
And then you should be able to get it with:
from main.Text_Generator import *
You're using a module outside your Django project. I would recommend moving the folder inside your project directory [or app directory] rather than messing with your PATH. If you move main inside backend your existing stuff will work.

Cannot import package from a different folder

I am working on a project based on Python 2.7 and I am trying to import a module which is in a package folder that contains __init__.py and the file that I want to import called package1.py, but I am unable to do so. This is my folder structure: main_project/Tools/common/package1.py
Note that my project files are in the folder main_project. So, I am trying to call the package1.py by using an import statement in my script:
from Tools.common.package1 import variable
But I am getting an ImportError: No module named Tools.common.package1.
What is the solution to solving this error as I want to use the package feature for my project?
Maybe use the solution i found here :
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
or verify your module has a __init__.py
Importing files from different folder
Ok I have found the answer. I also had to insert an init.py in the folder Tools as well. Initially I only inserted init.py in common but not in Tools as we should also make Tools a package if we want to access common

Import custom module to Django

I'm making a django app and cannot import a custom module inside the views.py file.
I'm trying to import class "Integrate" from auth.py inside modules folder from integrations/views.py
I tried placing init.py inside the app folder and modules folder but still doesn't work.
views.py:
from ..modules.auth import Integrate
Powershell:
from ..modules.auth import Integrate
ValueError: Attempted relative import beyond toplevel package
I do this a lot in my projects. Creating custom modules and importing them.
Try this:
from modules.auth import Integrate
You can use this :
from .models import Integrate
It will be work for use models
I am pretty certain I had this exact problem.
It helps if you make the directory holding the module a 'Sources Root'
You right click on the directory and down the bottom of the pop-up is 'Mark Directory As' option.
AFAIK this adds that directory to the PythonPath so the module in there will be found.

global/app lib folders import in Django

I have a Django structure like this (only showing libs):
project/lib/ # Global libraries that will be used cross apps
project/lib/global_stuff.py
project/apps/app1/lib/special_for_app1.py
project/apps/app2/lib/special_for_app2.py
Some apps don't have a lib folder.
from apps.app1.lib import special_for_app1 works fine. But how can I import from the global lib folder when I am inside a folder already containing a local lib folder?
From inside the apps views.py file on one of the apps:
from lib import global_stuff
Gives me ImportError: cannot import name global_stuff
from .lib import global_stuff
Gives me ImportError: cannot import name global_stuff
from ..lib import global_stuff
Gives me ImportError: No module named lib
from ...lib import global_stuff
Gives me ValueError: Attempted relative import beyond toplevel package
from project.lib import global_stuff
Works, but I really dont want to be stuck with using the project name itself in the import.
Is there any way to solve this, not using the project name in the import, or changing the whole lib idea.
Or is there any other good practice to store the main part of the code?
You are correct for not wanting to associate the project name with the imports, so there is a common pattern for this:
project/
|__/source/
| |__/lib/
| |__/app/
|__/deployment/ # code for your production deployment maybe
|
|__/docs/
|__/tests/
|__README
|__requirements.txt
and put /path/to/project inside your paths on your virtualenv(you use virtualenv right?).
Then you can do inside your code
from source.lib.blah import foo
from source.app.baz import bar
EDIT: This is only optimal if you don't release your code as open source of course. Only when you have an internal project where the management keeps changing the name of the project :D
I really dont want to be stuck with using the project name itself in the import
Why not? It's the best way. Note that 'relative imports for intra-package imports are highly discouraged', - as said in PEP-8.

Including and referencing 3rd party libraries in a GAE project

For my gae python project, I'd like to import an external library named 'vobject'. What's the correct way to import it in my .py files?
The project's readme says that in order to install it, you need to run
python setup.py install
Additionally, vobject requires the 'dateutil' package.
Since this is going to run on GAE, I thought I should copy both libs over into my project instead of running the install script to make use of it in my code.
But I'm getting a bunch of import errors and I'm not sure what the correct convention is for external gae/python libs.
utc = dateutil.tz.tzutc()
## error produced:
File "myGaeProject/external/vobject/icalendar.py", line 47, in <module>
NameError: name 'dateutil' is not defined
Because of the way I've structured my project, I changed icalendar.py's import structure from:
import dateutil.rrule
import dateutil.tz
to:
import external.dateutil.rrule
import external.dateutil.tz
I also tried:
from external.dateutil import *
What's the correct import mechanism for a project structured like so:
-myGaeProject
--external
----__init__.py
----dateutil
------__init__.py
------tz.py
------rrule.py
------[more dateutil files]
----vobject
------__init__.py
------base.py
------icalendar.py
--handlers
------__init__.py
------mainHandler.py
Don't modify the library. If you want to put all your libraries in external, you need to add external to your python path before you attempt to import libraries from there:
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), 'external'))
import some_external_library
You can't do from external import dateutil if external is missing an __init__.py file.
The good way is to use zipimport, you can check the project jaikuengine they are a lot of good things about that.
http://code.google.com/p/jaikuengine/source/browse/trunk/build.py
In Jaiku, all external libs are stocked in the directory vendor but if you see the app.yaml, all files in vendor are skipped.
Jaiku uses a script to build a zip of each libs in vendor and put it to the root of the project before the deployment or when the dev_server is launched.
With that, you don't need to fix the path of your libs.
EDIT an example to load all zipped archives
Largely inspired from jaikuengine:
def load_zipped(path='.'):
for x in os.listdir(path):
if x.endswith('.zip'):
if not any([y.endswith(x) for y in sys.path]):
sys.path.append(os.path.abspath('%s/%s') % (path, x))

Categories