Reputation: 8932
I am new to Python so please be patient with me. I am installing all the usual packages via pip into a virtual environment with no problem. But then there are external packages that I am grabbing from github and my own library package lib. I want to add these to my project and I want to maintain versioning so I know when to update. All I see as I research is code like
current_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_path)
That adds the package directory to the path. There are also examples of import statements with relative paths.
Shouldn't these non-pip packages be installed straight into the virtual environment with proper versioning? How do I achieve that?
Upvotes: 0
Views: 271
Reputation: 7242
You can easily do that since pip supports installation from a version control system, see here.
This assumes that the package you wish to install has a setup.py file.
pip install git+git://github.com/BillMills/python-package-example.git
(Mostly extraction form here)
Pip supports cloning over git
, git+http
, git+https
, git+ssh
, git+git
and git+file
.
[-e] git://git.myproject.org/MyProject#egg=MyProject
[-e] git+http://git.myproject.org/MyProject#egg=MyProject
[-e] git+https://git.myproject.org/MyProject#egg=MyProject
[-e] git+ssh://git.myproject.org/MyProject#egg=MyProject
[-e] git+git://git.myproject.org/MyProject#egg=MyProject
[-e] git+file://git.myproject.org/MyProject#egg=MyProject
-e [email protected]:MyProject#egg=MyProject
You can also ask to install from a specific branch, commit hash, or tag name if you want a beta version or a branch that is targeted to your specific distribution etc by using @
and passing over the name of the branch/commit hash/tag name:
Branch:
[-e] git://git.myproject.org/MyProject.git@master#egg=MyProject
Commit Has:
[-e] git://git.myproject.org/MyProject.git@da39a3ee5e6b4b0d3255bfef95601890afd80709#egg=MyProject
Tag:
git://git.myproject.org/[email protected]#egg=MyProject
Let's say that we wish to install the following Python package that is available on GitHub here.
Simply I will run:
pip install git+git://github.com/BillMills/python-package-example.git
And here is the result:
(test) pc-207-126:Desktop rafael$ pip install git+git://github.com/BillMills/python-package-example.git
Collecting git+git://github.com/BillMills/python-package-example.git
Cloning git://github.com/BillMills/python-package-example.git to /private/var/folders/c_/8qcnm5sj3kg7_f887qv473tm0000gn/T/pip-mx1vcsod-build
Collecting numpy (from python-package-example==0.1)
Downloading numpy-1.14.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.7MB)
100% |████████████████████████████████| 4.7MB 305kB/s
Installing collected packages: numpy, python-package-example
Running setup.py install for python-package-example ... done
Successfully installed numpy-1.14.1 python-package-example-0.1
If I now run pip freeze
to see if the package was installed:
(test) pc-207-126:Desktop rafael$ pip freeze
numpy==1.14.1
python-package-example==0.1
As you can see the installation was successful and has also installed some dependencies of the Python-Package-Example (i.e numpy).
Note: github.com/BillMills/python-package-example.git uses Python 2 syntax. For an example containing Python 3 syntax, see https://github.com/kennethreitz/samplemod. The difference is in the import syntax in init, python 2 uses 'import somePython' while python 3 uses 'from . import somePython'
Upvotes: 1