web2py external libraries - python

how can i import other external libraries in web2py? is it possible to
load up libs in the static file?
can somebody give me an example?
thanks
peter

If the library is shipped with python, then you can just use import as you would do in regular python script. You can place your import statements into your models, controllers and views, as well as your own python modules (stored in modules folder). For example, I often use traceback module to log stack traces in my controllers:
import traceback
def myaction():
try:
...
except Exception as exc:
logging.error(traceback.format_exc())
return dict(error=str(exc))
If the library is not shipped with python (for example, pyodbc), then you will have to install that library (using distutils or easy_install or pip) so that python can find it and run web2py from the source code: python web2py.py. Then you will be able to use regular import statement as described above. Before you do this make sure you installed the library properly: run python interpreter and type "import library_name". If you don't get any errors you are good to go.
If you have a third party python module or package, you can place it to modules folder and import it as shown below:
mymodule = local_import('module_name')
You can also force web2py to reload the module every time local_import is executed by setting reload option:
mymodule = local_import('module_name', reload=True)
See http://web2py.com/book/default/section/4/18?search=site-packages for more information.

In web2py you import external library as you normally do in Python
import module_name
or
from module_name import object_name
I am not sure what you mean by "in the static file"

Related

Python: Module Import Error for the billionth time

I can't believe after 8 years and 10,000 hours of using Python that I still get stumped by module import errors!!!! My tree is as follows:
pcode
/general
/latin
/latin
v_reading.py
/other
add_path.py
use_pydub.py
In the add_path.py module I add some modules to the sys.path like so:
import sys
vol='/users/me/'
gen_dir = f'{vol}Documents/pcode/'
sys.path.append(gen_dir)
gen_dir1 = f'{vol}Documents/pcode/byu_corpus/'
sys.path.append(gen_dir1)
gen_dir1 = f'{vol}Documents/pcode/latin/'
sys.path.append(gen_dir1)
gen_dir1 = f'{vol}Documents/pcode/latin/latin/'
sys.path.append(gen_dir1)
gen_dir1 = f'{vol}Documents/pcode/general/'
sys.path.append(gen_dir1)
gen_dir1 = f'{vol}Documents/pcode/other/'
sys.path.append(gen_dir1)
so when I run sys.path I get inter alia:
/Users/me/Documents/pcode/other
/users/me/documents/pcode/
/users/me/documents/pcode/latin/ ## I'm not sure if this is necessary but I did it anyway
/users/me/documents/pcode/latin/latin/
/users/me/documents/pcode/general/
/users/me/documents/pcode/other/
The first lines of the use_pydub.py module are as follows:
import sys
import add_path
from general import *
import latin
from latin.latin.v_reading import focus
I do not get an error when I run the module from pycharm but I do get the following error when I run it from command line:
ModuleNotFoundError: No module named 'latin.latin'
I don't understand why I can import the 'latin' folder but not the 'latin.latin' folder. I also tried renaming the second latin folder to latin2 but that did not help. I should also point out that there is no difference between the sys.path generated by pycharm and the sys.path generated by command line.
Solve the problem by correctly using relative imports:
Delete add_path.py, as it will no longer be useful or relevant.
In use_pydub.py, the appropriate relative imports look like:
import sys
from ..general import *
from .. import latin
from ..latin.latin.v_reading import focus
Ensure that pcode is a top-level package when the code runs. If, for example, use_pydub.py is intended to be a top-level script, run it as a module instead, with the -m flag to Python. Alternately, have a top-level driver script that uses the pcode package appropriately.
Ensure that the package is on the module import path. This doesn't require hacking sys.path, in general. It requires starting the program from just outside pcode, in development; or you can simply rely on the fact that installing the package, per the usual process will put it into one of the default paths where Python looks for third-party libraries. (That's the point of all that packaging stuff, using PyPI and pip etc.)
These things are especially important if you want to package the code and upload it to PyPI for others to use. Absolute paths hacked into sys.path are not going to work on someone else's computer - with a different username, the code installed into who knows which Python installation located who knows where on the disk, etc. Hacking in relative paths is just clumsily reinventing the wheel.
Problem solved. By using the advice of tdelaney I ran print(latin.__file__) I discovered that I was not importing the correct package and that I had earlier downloaded my own package from pypi and placed that package in the site_packages and one of those packages was named latin. That packaged was interfering with importing of the desired package. So thanks for the tip tdelaney.

Unable to import requests module in python due to "ImportError: cannot import name utils"

I'm trying to import python module requests in a program that uses python (Choregraphe for NAO the robot). I can't use shell commands like sudo install etc... I can only import module by moving the module into lib folder of the project.
So I've downloaded requests from pypi, and I've also downoaded requirements that I've moved into requests folder (https://i.imgur.com/XXlSz0N.png).
But when I try to import requests from the program, it returns me an error:
File "C:\Users\gurfe\AppData\Roaming\PackageManager\apps\.lastUploadedChoregrapheBehavior\behavior_1\../lib\requests\__init__.py", line 112, in <module>
from . import utils
ImportError: cannot import name utils
Why do I see this error?
Including dependency libraries in your Choregraphe package can be tricky (you need to make sure they are compiled for the right architecture, and things will work differently for a virtual robot) - but first, did you make sure that these libraries are not already on the robot?
I know "requests" is included on Pepper; it may be included on Nao (I think it is but I don't have a handy Nao to check); if it is you don't need to worry about including it in your package (you may have to modify the pythonpath when running on a virtual robot... but in all cases you should be able to rely on a system requests without packaging it)
If you use Choregraphe you can do this:
Place the lib folder in your Choregraphe project folder.
Create a python script in Choregraphe and paste this in init:
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
self.logger.warning("import only works on physical robot")
behaviorPath = ALFrameManager.getBehaviorPath(self.behaviorId)
sys.path.append(behaviorPath)
k = behaviorPath.rfind("/")
packagePath = behaviorPath[:k]
sys.path.append(packagePath)
import utils
self.utils = utils

Importing Python modules for Azure Function

How can I import modules for a Python Azure Function?
import requests
Leads to:
2016-08-16T01:02:02.317 Exception while executing function: Functions.detect_measure. Microsoft.Azure.WebJobs.Script: Traceback (most recent call last):
File "D:\home\site\wwwroot\detect_measure\run.py", line 1, in <module>
import requests
ImportError: No module named requests
Related, where are the modules available documented?
Related question with fully documented answer
Python libraries on Web Job
You need to include a requirements.txt file with your code which lists all the python dependencies of your function
From the docs:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#python-version-and-package-management
For example, your reqirements.txt file would contain:
requests==2.19.1
Python support is currently experimental for Azure Functions, so documentation isn't very good.
You need to bring your own modules. None are available by default on Azure Functions. You can do this by uploading it via the portal UX or kudu (which is handy for lots of files).
You can leave comments on which packages you'd like, how you'd like to manage you packages here on the tracking issue for "real" Python support - https://github.com/Azure/azure-webjobs-sdk-script/issues/335
Install python packages from the python code itself with the following snippet:
def install(package):
# This function will install a package if it is not present
from importlib import import_module
try:
import_module(package)
except:
from sys import executable as se
from subprocess import check_call
check_call([se,'-m','pip','-q','install',package])
for package in ['requests','hashlib']:
install(package)
Desired libraries mentioned the list gets installed when the azure function is triggered for the first time. for the subsequent triggers, you can comment/ remove the installation code.

Having an issue creating an exe with py2exe and script importing xlrd

My goal is to create a python script that loops over cells of an excel document. This is my python script called reader.py, and it works just fine.
import xlrd
import os
exceldoc = raw_input("Enter the path to the doc [C:\\folder\\file.xlsx]: ")
wb = xlrd.open_workbook(exceldoc,'rb')
.... some code....
The problem I'm encountering is attempting to use py2exe to create an executable so this script can be used elsewhere.
Here is my setup.py file:
from distutils.core import setup
import py2exe
import sys
from glob import glob
setup(name='Excel Document Checker',console=['reader.py'])
I run the following command: python setup.py py2exe
It appears to run fine; it creates the dist folder that has my reader.exe file, but near the end of the command I get the following:
The following modules appear to be missing
['cElementTree', 'elementtree.ElementTree']
I did some searching online, and tried the recommendations here Re: Error: Element Tree not found, this changing my setup.py file:
from distutils.core import setup
import py2exe
import sys
from glob import glob
options={
"py2exe":{"unbuffered": True,"optimize": 2,
'includes':['xml.etree.ElementPath', 'xml.etree.ElementTree', 'xml.etree.cElementTree'],
"packages": ["elementtree", "xml"]}}
setup(name='Excel Document Checker',options = options,console=['reader.py'])
I'm now getting an error:
ImportError: No module named elementtree
I'm sort of at an impasse here. Any help or guidance is greatly appreciate.
Just some information - I'm running Python 2.6 on a 32 bit system.
You explicitly told setup.py to depend on a package named elementtree here:
"packages": ["elementtree", "xml"]}}
There is no such package in the stdlib. There's xml.etree, but obviously that's the same name.
The example you found is apparently designed for someone who has installed the third-party package elementtree, which is necessary if you need features added after Python 2.6's version of xml.etree, or if you need to work with Python 1.5-2.4, but not if you just want to use Python 2.6's version. (And anyway, if you do need the third-party package… then you have to install it or it won't work, obviously.)
So, just don't do that, and that error will go away.
Also, if your code—or the code you import (e.g., xlrd) is using xml.etree.cElementTree, then, as the py2exe FAQ says, you must also import xml.etree.ElementTree before using it to get it working. (And you also may need to specify it manually as a dependency.)
You presumably don't want to change all the third-party modules you're using… but I believe that making sure to import xml.etree.ElementTree before importing any of those third-party modules works fine.

How to find the location of a python package installed via egg-link?

I'm working on a plugin architecture and need to convert a package name like "foo.bar" to the absolute path where the code resides. imp.find_module seems to do what I want, except when the code in question is installed via an egg-link (installed via 'pip install develop').
If there are two modules foo.bar and foo.bar2 which are installed via egg-links (and which live at completely separate file system locations like /home/bob/foo/bar and /home/alice/foo/bar2), find_modules doesn't work because I look up the package "foo" and get the location to foo/bar, but not foo/bar2.
Anyone have suggestions for an alternative function? find_modules doesn't accept hierarchical names, so I can't just pass "foo.bar2" into it.
The easiest way would be to just import the module and inspect its __file__ attribute:
import os
import foo.bar
print(os.path.abspath(foo.bar.__file__))
For dynamic imports:
import os
import sys
module_name = 'foo.bar'
__import__(module_name)
print(os.path.abspath(sys.modules[module_name].__file__))

Categories

Resources