FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

Installing python modules




I have encountered a lot of problems trying to get modules work in the hosting, so i have thought it would be nice to share my experience and solutions.

-I need to use a module and cant be imported
First of all you have to have python installed in another computer. I use bash for windows 10, so i will put the commands that works on linux.I will be using python version 2.7.
After that, you have 2 options to get the module:
--You can install it via
Code:
pip install <module>
and after that you take the module from
Code:
/usr/local/lib/python2.7/dist-packages
compress it and upload it to frihost.
--You can compile it from source, which you can usually find in https://pypi.python.org. After you donwloaded it, extract it, cd into the folder and run
Code:
python setup.py build

[/code]
Then a build folder will be created and enter the folders inside until you find one with the same name as the module, zip it and upload it.

Now that you have a zipped version of your module in the hosting, you have to create folder for the modules. I personally created /home/<username>/public_html/libs but it can be anything that you can access. After creating the folder you have to unzip the module inside.

Afterwards, you have to include the following line of code before all the imports:
Code:
import sys
sys.path.append("/home/<username>/public_html/libs")

After this line you can put the necessary imports. Here is an example of one full code:
Code:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
sys.path.append("/home/restonpi/public_html/libs")
import mechanize
browser = mechanize.Browser()
browser.open("https://www.google.es/")
response=browser.reload()
print (response.read())

This is just a sample of using a module called mechanize to scrape a webpage.
It's important that any print that you make is in html format, if not a 500 error will show



0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.