How to Import Libraries into python project from GitHub - python

I am trying to create a project in python using some libraries from GitHub that I found. I am using InstaPy and Instagram-API-python.
My project structure is as follows
Project(main folder)
Instapy(sub-folder)
Instagram-API-python(sub-folder)
insta.py
How would I import both these into the insta.py file? I am new to python and not sure how the imports work.

Don't copy them into your project folder, use pip to install them on your Python path.
But first, read about virtual environments.
Once you have your virtual environment set up and activated you can install the packages thus:
$ pip install git+https://github.com/timgrossmann/InstaPy.git
$ pip install git+https://github.com/LevPasha/Instagram-API-python.git
Then, in your python script just import them
from instapy import InstaPy
import InstagramAPI
and use them.
I followed these instructions and it (mostly) worked. It looks like InstagramAPI needs an executable installed (ffmpeg) which I'm not wanting to install on my laptop. It reports the error during the initial import.
I could definitely import instapy .. but notice that it is all lower case.
But since there is only the InstaPy class in the instapy module, it's best to simply import it this way.
More on InstaPy here: https://github.com/timgrossmann/InstaPy

Related

Unable to fully package my Python script with all the modules and dependencies it needs for Linux

I have a Python 2.7 script that uses BeautifulSoup4 and requests modules.
The issue is, that I need to deploy this script on a machine to which we can not directly install any new modules/libaries via pip install or anything else.
We can copy this script and any files it needs to run to that machine, but we can not directly install any modules.
I have tried PyInstaller, PEX and Nuitka to create an executable file or a bundle (in any format, for example .zip) so that we can copy the entire file or bundle into the machine and run the python script from there, without the need to do pip install or installing the modules manually via Wheel file. All without success.
Environment details:
Target machine on which the script needs to run: RHEL-based Linux OS with Python 2.7.
My development machine: Windows 10 but I also have access to Fedora Linux machine both with Python 3 and Python 2.7.
The import section of my script looks like this:
from __future__ import with_statement
from __future__ import absolute_import
import requests
import re
from bs4 import BeautifulSoup
from io import open
Can someone, please, help me out here?
We have the script ready to be deployed, but we are not able to run it in our target machine because of the missing modules/libraries.
Thank you very much
EDIT:
Mentioning this since it may not be clear at first - we do not have the issue with a network connection or anything of this gender. We were prohibited to use pip install or a manual method of installing a module. Therefore, we can only bundle the modules directly with the script or something so that would not need to directly install the modules on the target machine itself.

Python Linux Google drive API client install modules not found

I'm trying to start working with google drive API from python on my local Linux machine. I want to be able to move files back and forth to Google drive from my local machine. I just started learning Python yesterday and I'm having problems with the Google quickstart instructions. when I try to run the quickstart code at https://developers.google.com/drive/v3/web/quickstart/python with python3 I get this error:
ImportError: No module named 'apiclient.discovery'
which results from these import statements at the top of the file.
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
As per the instructions I installed what I thought i needed with the command
pip install --upgrade google-api-python-client
But I'm getting this error. I notice if i comment out the line
from apiclient.discovery import build
then the rest of the import statements are ok, and the script starts executing and browser pops up asking for authentication, but then obviously when it reachs the 'build' function call in the script it breaks. What am I doing wrong?
I've tried installing the lib folder containing the modules to the local directory where my script is executing, but I think that's only important if you're using google app engine which I am .... not? I don't think I am right? When i did that i tried changing the import statement to
from lib.googleapiclient.discovery import build
I get the same error, but I've actually opened up that file in lib/googleapiclient/discovery.py in my local directory and in the code is
...
def build(serviceName,
version,
http=None,
discoveryServiceUrl=DISCOVERY_URI,
developerKey=None,
model=None,
requestBuilder=HttpRequest,
credentials=None,
cache_discovery=True,
cache=None):
...
So why in the world wouldn't this import statement work? I've also tried changing the statements from
from apiclient.discovery import build
to
from googleapiclient.discovery import build
and I get the same error, No module named 'googleapiclient'
I've installed 3rd party modules yesterday with pip and I never had any problems. What's going on? Thanks for your help.
Check your python and pip version, and try pip freeze to check the installed package.
python --version
pip --version
pip freeze
Please check your python version, where your pip is from (/usr/lib/python2.7 or /usr/lib/python3.6 or the other virtualenv) and whethergoogle-api-python-client is in the output of pip freeze.
Try to reinstall google-api-python-client.
sudo pip install --force-reinstall google-api-python-client
apiclient is the old name, so it is better to use googleapiclient for import.
from googleapiclient.discovery import module

How to add a non-Django python package to a Django project?

This is probably one of those annoying newbie questions. I'm trying to use a python package called jieba in my Django project. I've tried pip install and dragging the package folder into my apps directory, but have not succeeded in importing the package (ModuleNotFoundError). Please tell me how this could be done. Thanks!
Edit:
I mean I tried pip install jieba, and it didn't work (ModuleNotFoundError).
I made sure that it was correctly installed in my project virtual environment, but don't know how to import that in the Django project. Tried: import jieba and from jieba import jieba, no luck.
Then, I tried dragging that folder to the apps directory, and it still didn't work.
Update:
It turns out I have correctly installed jieba, but Atom Runner somehow cannot import it. I switched to PyCharm, and now it works fine. Nothing has changed except the editor.
Most likely you can't do:
import jieba
anymore because you've dragged/moved the package folder from where it supposed to be after pip installing it. Try to drag it back or uninstall and install package again, then import as normal.
Also if you are using virtual env make sure you activated your env before installing the package.
You don't need to copy python package from dist to your project root.
Pip packages is works standalone and not need to implement for django.
this is mean that after you need to only install your package with pip and use in project like following sample :
pip install jieba
# encoding=utf-8
import jieba
seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
print("Full Mode: " + "/ ".join(seg_list)) # 全模式
seg_list = jieba.cut("我来到北京清华大学", cut_all=False)
print("Default Mode: " + "/ ".join(seg_list)) # 精确模式
seg_list = jieba.cut("他来到了网易杭研大厦") # 默认是精确模式
print(", ".join(seg_list))
seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在日本京都大学深造") # 搜索引擎模式
print(", ".join(seg_list))

can't import google.appengine.api

This should be pretty basic as I've installed lots of python packages, but I can't for the life of my get google apis client library for python to install.
I'm pressure sure I've gone through the instructions on this website properly:
https://developers.google.com/api-client-library/python/start/installation
To summarize I've done the following:
$ easy_install --upgrade google-api-python-client
this seems to work fine, doesn't report any errors or warnings
downloaded and unzipped google-api-python-client-gae-1.2.zip into the directory where my project is
but if I open an iPython session in the folder where I unpacked the full dependencies I can't do the basic imports such as:
import google.appengine.api it just says "No Module named google.appengine.api"
I checked in my site-packages folder and google_api_python_client-1.2-py2.7.egg is there. But it doesn't show up in sys.path
when I do sys.path.append('C:\Anaconda\Lib\site-packages\google_api_python_cli
ent-1.2-py2.7.egg') it adds the correct path, but the import still doesn't work.
EDIT: This fixed my problem
Adding the Google SDK to my Python path did the trick. I don't know why the installer didn't do this when I ran it. but hey, this worked
So if I run:
sys.path.append('C:\\Program Files (x86)\\Google\\google_appengine')
from google.appengine import api
works!
Without knowing all the steps you took to install app engine and the client APIs, my only recommendation is to install (or reinstall) the Google App Engine Python SDK from here: https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python. Looks like you're on windows, so you would grab the MSI.
Just make sure you launch the AppEngineLauncher application after installation as it will give you the option to create symlinks so you can run commands from terminal.

How to solve "Unresolved import: HTML" in Python Development?

I'm starting to learn about python development in a new project.
I got setup almost everything right, but only this import HTML that keeps given me some error that I don't know how to solve it.
import web
import json
from WebServer.forms import mainPageForm, addBugForm, addProblemForm, addProblemTypeForm, versionsDropdownForm,\
severitiesDropdownForm, problemTypesDropdownForm, problemsDropdownForm
import BugRecorderCore.controller as ctrl
import BugRecorderCore.validators as vdt
import datetime
import os
from BugRecorderCore.utils import concatenateString
import HTML
//...
I already tried to install HTML.py already, but still no success so far.
Any idea or advice about this issue ?
UPDATE
Following the suggestions from the answers below I got this message:
It looks like you are using anaconda, have you tried installing it the anaconda way?
conda install HTML
Also do you by any chance have 2 version of Python on your system?
If the package is unavailable you'll have to user pip. If you don't have pip, from your command line write:
python get-pip.py
pip install HTML
Looking the given screenshots and tags I suppose your are using Anaconda (which I have no experience but it is still Python, anyway) and the IDE is not resolving the import.
Make sure you have installed/updated HTML.py
conda install HTML
At your IDE go to Window > Preferences > Python Interpreter
At Libraries tab make sure you have the following folders added to your PYTHONPATH:
C:\Anaconda\Lib
C:\Anaconda\Lib\site-packages
C:\Anaconda\Scripts
That should do the trick.
Important: try to always install your libraries through conda (or pip when using Python directly). It will install things where it should be. ;)

Categories