How to make a private module pip installable? - python

I have python package which need to be installed to run a Django project?
I go into the python virtual environment and clone the module from git in site-packages folder inside lib.
What i need is to make that module pip intallable and installation access should be given only to specific people i.e that module should not be public to everyone.

Build the python package as you normally would for a public build. For helpful step-by-step instructions on that front, check out the python docs
There are a number of ways to maintain both installability and privacy. When I looked into this for my own packages I started with the suggestions at this site. This site includes instructions on how to build your own equivalent of a PyPi server.
The solution I landed on though, I feel is quite simpler. I pushed the entire package to a private git repository hosted on github. You can then install using pip install git+[insert full url to your github repository here]. You can enforce privacy by restricting who has access to your git repository.

To make your package part of the requirements, place it where it will be accessible only by the people you want to have access, e.g. on a private github. Then you can add a line like this to any project's requirements.txt, and pip will fetch it and install it:
-e git://github.com/<user>/<package>.git#egg=<package>
(Replace with the name of the package you are distributing.) This line is simply added to the list of simple package names that requirements.txt usually contains. You could also replace the git download with an egg placed on a local fileshare, or whatever.

Related

How can I edit a GitHub repository (for a Python package) locally and run the package with my changes?

I have cloned a GitHub repository that contains the code for a Python package to my local computer (it's actually on a high performance cluster). I have also installed the package with pip install 'package_name'. If I now run a script that uses the package, it of course uses the installed package and not the cloned repository, so if I want to make changes to the code, I cannot run those. Is there a way to do this, potentially with pip install -e (but I read that was deprecated) or a fork? How could I then get novel updates in the package to my local version, as it is frequently updated?
If you run an IDE like PyCharm, you can mark a folder in your project as Sources Root. It will then import any packages from that folder instead of the standard environment packages.
In the end I indeed did use pip install -e, and it is working for now. I will figure it out once the owner of the package releases another update!

How to install a private repository starting from a public GitHub project

I need to work with the pyzx package; which is a public.
I want to extend it with some functions of mine. But I need these to be private.
So a normal fork can't work.
I have to copy the project in a private repository and then installing it.
I managed to create such a repository, but now I can't install it.
My attempts result with an error or with the installing of the original repository.
How can I install my private repository?
I work on the Anaconda-Jupyter IDE. I tried to specify the path:
pip install '/Users/danielecuomo/Documents/Github/mypyzx/pyzx-master/'
But when I call my personal function, I get the following error:
ImportError: cannot import name 'extract_normal_form_circuit' from 'pyzx' (/Users/danielecuomo/opt/anaconda3/lib/python3.9/site-packages/pyzx/__init__.py)

packaging scientific project in python

I am trying to build a package for an apps in python. It uses sklearn, pandas, numpy, boto and some other scientific module from anaconda. Being very unexperienced with python packaging, I have various questions:
1- I have some confidential files .py in my project which I don't want anyone to be able to see. In java I would have defined private files and classes but I am completely lost in python. What is the "good practice" to deal with these private modules? Can anyone link me some tutorial?
2- What is the best way to package my apps? I don't want to publish anything on Pypi, I only need it to execute on Google App engine for instance. I tried a standalone package with PyInstaller but I could not finish it because of numpy and other scipy packages which makes it hard. Is there a simple way to package in a private way python projects made with anaconda?
3- Since I want to build more apps in a close future, shall I try to make sub-packages in order to use them for other apps?
The convention is to lead with a single underscore _ if something is internal. Note that this is a convention. If someone really wants to use it, they still can. Your code is not strictly confidential.
Take a look at http://python-packaging-user-guide.readthedocs.org/en/latest/. You don't need to publish to pypi to create a Python package that uses tools such as pip. You can create a project with a setup.py file and a requirements.txt file and then use pip to install your package from wherever you have it (e.g., a local directory or a repository on github). If you take this approach then pip will install all the dependencies you list.
If you want to reuse your package, just include it in requirements.txt and the install_requires parameter in setup.py (see http://python-packaging-user-guide.readthedocs.org/en/latest/requirements/). For example, if you install your package with pip install https://github/myname/mypackage.git then you could include https://github/myname/mypackage.git in your requirements.txt file in future projects.

How to install python from source without internet connection?

I'm installing python on custom location on a internal server.
Unfortunately, I can't make full internet connection here. Most of sites are block by firewall. (essentially pypi repository!) Please don't ask the reason. And I don't have root account, so I have to install python from source.
I did install python from source successfully! But the problem is any of easy_install or pip is not installable because the sites are not accessible form here. :(
How can I install them under current situation?
Download the source tarballs of the module you are interested in to your PC
Upload it to the remote server (SCP)
Extract the tarball (eg. tar -xvzf pack.tar.gz)
Set the current directory to the extracted folder (should contain a file named setup.py)
Install the module: python setup.py install (See documentation)
In my eyes setting up a local mirror like proposed by Tim is not worth of it (Of course this depends on how often you plan to install/update Python modules on that remote machine...)
Download the source tarballs of the relevant modules and install them locally.
easy_install /your/file/location/pack.tar.gz

Best practice for installing python modules from an arbitrary VCS repository

I'm newish to the python ecosystem, and have a question about module editing.
I use a bunch of third-party modules, distributed on PyPi. Coming from a C and Java background, I love the ease of easy_install <whatever>. This is a new, wonderful world, but the model breaks down when I want to edit the newly installed module for two reasons:
The egg files may be stored in a folder or archive somewhere crazy on the file system.
Using an egg seems to preclude using the version control system of the originating project, just as using a debian package precludes development from an originating VCS repository.
What is the best practice for installing modules from an arbitrary VCS repository? I want to be able to continue to import foomodule in other scripts. And if I modify the module's source code, will I need to perform any additional commands?
Pip lets you install files gives a URL to the Subversion, git, Mercurial or bzr repository.
pip install -e svn+http://path_to_some_svn/repo#egg=package_name
Example:
pip install -e hg+https://rwilcox#bitbucket.org/ianb/cmdutils#egg=cmdutils
If I wanted to download the latest version of cmdutils. (Random package I decided to pull).
I installed this into a virtualenv (using the -E parameter), and pip installed cmdutls into a src folder at the top level of my virtualenv folder.
pip install -E thisIsATest -e hg+https://rwilcox#bitbucket.org/ianb/cmdutils#egg=cmdutils
$ ls thisIsATest/src
cmdutils
Are you wanting to do development but have the developed version be handled as an egg by the system (for instance to get entry-points)? If so then you should check out the source and use Development Mode by doing:
python setup.py develop
If the project happens to not be a setuptools based project, which is required for the above, a quick work-around is this command:
python -c "import setuptools; execfile('setup.py')" develop
Almost everything you ever wanted to know about setuptools (the basis of easy_install) is available from the the setuptools docs. Also there are docs for easy_install.
Development mode adds the project to your import path in the same way that easy_install does. An changes you make will be available to your apps the next time they import the module.
As others mentioned, you can also directly use version control URLs if you just want to get the latest version as it is now without the ability to edit, but that will only take a snapshot, and indeed creates a normal egg as part of the process. I know for sure it does Subversion and I thought it did others but I can't find the docs on that.
You can use the PYTHONPATH environment variable or symlink your code to somewhere in site-packages.
Packages installed by easy_install tend to come from snapshots of the developer's version control, generally made when the developer releases an official version. You're therefore going to have to choose between convenient automatic downloads via easy_install and up-to-the-minute code updates via version control. If you pick the latter, you can build and install most packages seen in the python package index directly from a version control checkout by running python setup.py install.
If you don't like the default installation directory, you can install to a custom location instead, and export a PYTHONPATH environment variable whose value is the path of the installed package's parent folder.

Categories