Run Python package from files and not egg - python

I downloaded a Python package from Github. To use it I had to install an python egg on my computer. I'd like to make some modifications to the package. To do so, I have to recompile the egg each time to test it.
My question is, how do I run the python files directly without having to recompile the egg each time?

You can use an "editable" installation to link to the files directly, without having to build the egg each time.
$ pip install -e ./path/to/package
https://pip.pypa.io/en/stable/reference/pip_install/#editable-installs

Related

Run bash script with pip install

I have created a wrapper for c++ library using python.
I have a bash script that will download c++ code, compile it and copy the generated .so file to the python package.
Then I have written the setup.py so that when you use pip install package-name, it will install the .so file also.
What I need is to run that bash script when I type pip install package-name. Currently what I have to do is run the bash script first and then use pip install package-name to install.
And I need to upload the code to the Python Package Index. So the solution has to be compatible with that also*.
PS: I know about the Extension module in setuptools. I can't use that because I need to download something and run a Makefile after editing it. I do all of that with the bash script.
* I learnt that this requirement is not possible. So please ignore that one.
Thank you..

When would the -e, --editable option be useful with pip install?

When would the -e, or --editable option be useful with pip install?
For some projects the last line in requirements.txt is -e .. What does it do exactly?
As the man page says it:
-e,--editable <path/url>
Install a project in editable mode (i.e. setuptools "develop mode") from a local project path or a VCS url.
So you would use this when trying to install a package locally, most often in the case when you are developing it on your system. It will just link the package to the original location, basically meaning any changes to the original package would reflect directly in your environment.
Some nuggets around the same here and here.
An example run can be:
pip install -e .
or
pip install -e ~/ultimate-utils/ultimate-utils-proj-src/
note the second is the full path to where the setup.py would be at.
Concrete example of using --editable in development
If you play with this test package as in:
cd ~
git clone https://github.com/cirosantilli/vcdvcd
cd vcdvcd
git checkout 5dd4205c37ed0244ecaf443d8106fadb2f9cfbb8
python -m pip install --editable . --user
it outputs:
Obtaining file:///home/ciro/bak/git/vcdvcd
Installing collected packages: vcdvcd
Attempting uninstall: vcdvcd
Found existing installation: vcdvcd 1.0.6
Can't uninstall 'vcdvcd'. No files were found to uninstall.
Running setup.py develop for vcdvcd
Successfully installed vcdvcd-1.0.6
The Can't uninstall 'vcdvcd' is normal: it tried to uninstall any existing vcdvcd to then replace them with the "symlink-like mechanism" that is produced in the following steps, but failed because there were no previous installations.
Then it generates a file:
~/.local/lib/python3.8/site-packages/vcdvcd.egg-link
which contains:
/home/ciro/vcdvcd
.
and acts as a "symlink" to the Python interpreter.
So now, if I make any changes to the git source code under /home/ciro/vcdvcd, it reflects automatically on importers who can from any directory do:
python -c 'import vcdvcd'
Note however that at my pip version at least, binary files installed with --editable, such as the vcdcat script provided by that package via scripts= on setup.py, do not get symlinked, just copied to:
~/.local/bin/vcdcat
just like for regular installs, and therefore updates to the git repository won't directly affect them.
By comparison, a regular non --editable install from the git source:
python -m pip uninstall vcdvcd
python -m pip install --user .
produces a copy of the installed files under:
~/.local/lib/python3.8/site-packages/vcdvcd
Uninstall of an editable package as done above requires a new enough pip as mentioned at: How to uninstall editable packages with pip (installed with -e)
Tested in Python 3.8, pip 20.0.2, Ubuntu 20.04.
Recommendation: develop directly in-tree whenever possible
The editable setup is useful when you are testing your patch to a package through another project.
If however you can fully test your change in-tree, just do that instead of generating an editable install which is more complex.
E.g., the vcdvcd package above is setup in a way that you can just cd into the source and do ./vcdcat without pip installing the package itself (in general, you might need to install dependencies from requirements.txt though), and the import vcdvcd that that executable does (or possibly your own custom test) just finds the package correctly in the same directory it lives in.
From Working in "development" mode:
Although not required, it’s common to locally install your project in
“editable” or “develop” mode while you’re working on it. This allows
your project to be both installed and editable in project form.
Assuming you’re in the root of your project directory, then run:
pip install -e .
Although somewhat cryptic, -e is short for
--editable, and . refers to the current working directory, so together, it means to install the current directory (i.e. your
project) in editable mode.
Some additional insights into the internals of setuptools and distutils from “Development Mode”:
Under normal circumstances, the distutils assume that you are going to
build a distribution of your project, not use it in its “raw” or
“unbuilt” form. If you were to use the distutils that way, you would
have to rebuild and reinstall your project every time you made a
change to it during development.
Another problem that sometimes comes up with the distutils is that you
may need to do development on two related projects at the same time.
You may need to put both projects’ packages in the same directory to
run them, but need to keep them separate for revision control
purposes. How can you do this?
Setuptools allows you to deploy your projects for use in a common
directory or staging area, but without copying any files. Thus, you
can edit each project’s code in its checkout directory, and only need
to run build commands when you change a project’s C extensions or
similarly compiled files. You can even deploy a project into another
project’s checkout directory, if that’s your preferred way of working
(as opposed to using a common independent staging area or the
site-packages directory).
To do this, use the setup.py develop command. It works very similarly
to setup.py install, except that it doesn’t actually install anything.
Instead, it creates a special .egg-link file in the deployment
directory, that links to your project’s source code. And, if your
deployment directory is Python’s site-packages directory, it will also
update the easy-install.pth file to include your project’s source
code, thereby making it available on sys.path for all programs using
that Python installation.
It is important to note that pip uninstall can not uninstall a module that has been installed with pip install -e. So if you go down this route, be prepared for things to get very messy if you ever need to uninstall. A partial solution is to (1) reinstall, keeping a record of files created, as in sudo python3 -m setup.py install --record installed_files.txt, and then (2) manually delete all the files listed, as in e.g. sudo rm -r /usr/local/lib/python3.7/dist-packages/tdc7201-0.1a2-py3.7.egg/ (for release 0.1a2 of module tdc7201). This does not 100% clean everything up however; even after you've done it, importing the (removed!) local library may succeed, and attempting to install the same version from a remote server may fail to do anything (because it thinks your (deleted!) local version is already up to date).
As suggested in previous answers, there is no symlinks that are getting created.
How does '-e' option work? -> It just updates the file "PYTHONDIR/site-packages/easy-install.pth" with the project path specified in the 'command pip install -e'.
So each time python search for a package it will check this directory as well => any changes to the files in this directory is instantly reflected.

Install local dist package into virtualenv

I have a pytest test, let's call it test.py. I used to run this test outside of virtualenv; now I'm trying to run it inside a virtualenv sandbox.
The project is structured like this:
~/project/test # where test.py and all virtualenv files live
~/project/mylibrary
test.py imports from mylibrary. In the past, this worked because I have the code in ~/project/mylibrary installed into /usr/lib/python2.7/dist-packages/mylibrary.
I can't run virtualenv with the --system-site-packages flag. I also can't move the code from ~/project/mylibrary into the ~/project/test folder. How can I get access to the code in mylibrary inside my virtualenv?
You don't need to do anything special - as long as you are working inside a virtualenv, python setup.py install will automatically install packages into
$VIRTUAL_ENV/lib/python2.7/site-packages
rather than your system-wide
/usr/lib/python2.7/dist-packages
directory.
In general it's better to use pip install mylibrary/, since this way you can neatly uninstall the package using pip uninstall mylibrary.
If you're installing a working copy of some code that you're developing, it might be a good idea to install it in "editable" mode using pip install -e mylibrary/, which creates a link to your source directory so that your installed module gets updated as you edit the code.
The easiest way would be to add the directory containing the library to your sys.path

How to install library with pip-console?

I downloaded some python library package (for example uncompile2) from GitHub. How I can install it into Qpython from the directory in my android device with pip-console? Or at least to try install...
You could install it directly through pip console, just type "pip install "
Or, you can make a .sh file with contents
python /your/path/to/the/package/setup.py install
Or at least I think so.
You could copy the modules you want installed into the site-packages folder
using a file manager open up the folder:
/sdcard/com.hipipal.qpyplus/lib/python2.7/site-packages/
that is where you can put the modules, i have not tried it with the pip console.

How to install Python 3rd party libgmail-0.1.11.tar.tar into Python in Windows XP home?

I do not know Python, I have installed it only and downloaded the libgmail package. So, please give me verbatim steps in installing the libgmail library. My python directory is c:\python26, so please do not skip any steps in the answer.
Thanks!
The easiest way might be to install easy_install using the instructions at that page and then typing the following at the command line:
easy_install libgmail
If it can't be found, then you can point it directly to the file that you downloaded:
easy_install c:\biglongpath\libgmail.zip
Extract the archive to a temporary directory, and type "python setup.py install".
All you have to do is extract it, and put it somewhere (I prefer the Libs folder in your Python directory). Then read the readme. It explains that you need to do:
python setup.py
in your command line. Then you're done.
Let's say you downloaded and unzipped it to C:/libgmail-0.1.11. Open a command prompt and:
cd C:/libgmail-0.1.11
Then build an Windows installer:
python setup.py bdist --format=wininst
Then go to C:/libgmail-0.1.11/dist and you'll find an installer. Double click it, follow the "next" procedure and you're done.
What's nice about this method is that you can easily uninstall the library from Control Panel/Add or Remove Programs.
Here's how I did it:
Make sure C:\Python26 and C:\Python26\scripts are both on your system path.
Install setuptools. You'll have to download the source distribution, and extract it. You will likely need something like 7zip for this. If you use 7zip note that you will need to extract it twice. Once to get a .tar file, and again to get a directory out of that tar file.
Open a command prompt and cd to the directory you created. Run the command python setup.py install.
Run the command easy_install mechanize.
Install libgmail just like you did setuptools.
This was a lot of work, but you now have the easy_install tool available to simplify installing these kinds of things in the future. If you're doing anything serious, you may also want to consider setting up a virtualenv and using pip instead of easy_install.

Categories

Resources