Develop an Existing Python Package - python

I am trying to fix some bugs of an open source package on Github. So I git clone the fork to my local directory. The question is that how I can override the installed version with the version that I am developing.
Note:
1. This particular package does not support the setup.py develop command
2. This particular package is managed by Anaconda's conda. I have other packages in the same situation that is managed by pip

When you install a package it resides in <env dir>lib/python3.4/site-packages/<Any package> or you can place it in your app and import it in code.
If you have mongoengine code in your my_lib directory you can import it like
from my_lib import mongoengine

I figured this out. Just use:
pip install -e

Related

How to package python modules that are not in the standard Library

I have a script that I would like distributed on other machines, however it has a dependent module that is not a part of the python standard library. More specifically I want to use the pymysql library. When I am on my local machine I can just use pip install pymysql, but want to know if there are other options in packaging/distributing my script so that others do not have to run pip install pymysql.
My current error on other machines is:
ImportError: No module named pymysql
You can define dependencies inside your setup.py file. Try doing something like this:
setup(..., install_requires=['pymysql'])
If you are using setuptools to package your library. Here is some documentation: https://python-packaging.readthedocs.io/en/latest/dependencies.html
This question may help you some more: How to specify dependencies when creating the setup.py file for a python package
By doing this, if anyone installs your library, it will automatically install all the dependencies too.
If you need to define dependencies for a GitHub repository, you can define a requirements.txt file and keep it with the main code inside the repository (not inside any folder). This is how yours might look:
pymysql
It's just 1 line containing the requirements. If you have any more, simply type them on the next line:
pymysql
numpy
pandas
If anyone clones your repository, they will need to execute the following command in the directory in which they have cloned the files:
python3 -m pip install -r requirements.txt

Install git python library through python regardless of OS

I have developed a tool that my team can use after running the setup.py script. The tool requires this library: https://github.com/c2nes/javalang
How can I make my python setup script install this library on their computer regardless of what OS they are on. They can't run my tool without that library (Some people are on windows, mac, and linux.)
pip can install projects on Github as a dependency too!
All you need to do is, in your requirements.txt, add a line like following:
..
git+https://github.com/c2nes/javalang.git
then install the dependency using:
$ pip install -r requirements.txt
What you are looking for exists on PyPI. Instead of git+https://.. line above, just say: javalang. Oh and BTW, unless they are running old versions of Python, they should already have pip installed. If they don't use your operating systems package manager or get-pip.py as you said.

Import a Python library from Github

I'm new to Python so this may sound silly.
I want to use a Python library I've found on Github, lets say on https://github.com/praw-dev/praw, and I want to be able to do git pull in the future to pull the latest commits.
Question: Should I git clone <git url> in the project directory and delete everything except the praw directory, then in my python script do a import praw?
In iPython,
import praw
gives the error ImportError: No module named praw
Directory Structure
~\myProject\
praw\
myNotebook.ipynb
Actually, if given package is not on PyPI (or you want a specific branch) you can still install it through pip from GitHub with:
pip install git+https://github.com/[repo owner]/[repo]#[branch name]
And for your problem it would be (although #pandita's answer is correct for normal usage case):
pip install git+https://github.com/praw-dev/praw.git
For more information check this answer.
Experimental Python module finder/loader from github, like in golang.
So, in golang we can import like:
import "github.com/parnurzeal/gorequest"
But in python we should install package by our hands:
pip install requests
And import it like:
import requests
But with this magic package and power of PEP-0302 we can do it automatically:
from github_com.kennethreitz import requests
assert requests.get('https://github.com/nvbn/import_from_github_com').status_code == 200
Installation
You should have git, Python 3.2+ and pip:
pip install import_from_github_com
Reference: https://github.com/nvbn/import_from_github_com
Just clone the files in any dir on your python path and then build the lib typically with python setup.py install from the command line.
I typically clone a libray form git in my site_libraries folder ( the folder that holds all of your pip installed packages ). From there you can pull and then build the libraries from git just like any other git repo. Having the files there is nice because all of your libs are in once place on your python path.
You might want to consider using pip instead of git to install and upgrade the package (that is unless you have a pressing reason to use git).
pip install praw
to update the package you can do
pip install --upgrade praw
Also have a look here for further information on how to use pip.

How can I make setup tools install a github forked PyPI package?

Here is the example scenario.
There is a python package not-mine and I have just found a small bug in it. I find the source code on github and fork the repository. I make the necessary changes and submit a pull request. Unfortunately the package author is on vacation and I have a deadline.
I need a way to install my forked repository rather than the authors version living on PyPI. I have tried the following with no success:
install_requires = [
'not-mine==1.0.0'
],
dependency_links = [
'http://github.com/my-username/not-mine/tarball/master#egg=not-mine-1.0.0'
]
What am I missing?
Resources I have stumbled on while investigating the issue:
How can I make setuptools install a package that's not on PyPI?
You should be able to point pip at the URL of your forked repo with your bugfix because pip can install directly from git repos.
$ pip install git+git://github.com/my-username/not-mine#egg=not-mine
You can modify the pip install command to specify a particular commit, branch, tag, etc. with the "#" symbol before the "#".
$ pip install git+git://github.com/my-username/not-mine#bugfix_branch#egg=not-mine
If you just want to install your forked forked package on your system you can simply clone the package to your system and use python setup.py install command to install that package locally on your system.
If you need to deploy application with your own modified package then, i recommend you to use the python virtual environment
You can fork the project and locally clone it to your system. Then refer to this local fork via following:
pip install -e ~/Repositories/some_local_fork_repo_path/
Where "~/Repositories/some_local_fork_repo_path/" is path to your local cloned forked project. Then you can control what branch, etc. details on the other local cloned forked project independently of this project by simply working on that local project vs. this local project.

Correct way to integrate a Python module into my code (Hg, Google Code)

I had a custom script programmed and it is using the authors own module that is hosted on Google code in a Mercurial repo. I understand how to clone the repo but this will just stick the source into a folder on my computer. Is there a proper way to add the module into my python install to make it available for my projects? (e.g. with modules hosted on pypi you can use virtualenv and pip to install).
Thanks
Dave O
In exactly the same way. Just pass the address of the repo to pip install, using the -e parameter:
pip install -e hg+http://code.google.com/path/to/repo
If the module isn't on pypi, clone the repository with Hg and see if there's a setup.py file. If there is, open a command prompt, cd to that directory, and run:
python setup.py install

Categories