Installing a Python extension from scratch - python

I'm new to Python. I'm familiar with pip... but how do I install something that pip doesn't know about?
Specifically, this is a Jupyter SQL Magics extension (GitHub link)
How do I install an extension "from scratch"?

Not everything is uploaded to pip and can be installed by it.
This seems to be a python script not extension.
Just download the code and use it as import to your script.
Here is how imports work
Python extensions, modules, library or programs (call them how you like) have setup.py script that makes them install-able. Or if its a program that requires library in order to use it it might have requirements.txt file.
This file is used by pip to install every dependency like so:
pip install -r requirements.txt
you can read more about setup.py here
Can you give more details what are you trying to accomplish?

Related

How to install a python program using setup.py

I am trying to install labelImg using setup.py. I run the command:
sudo python3 setup.py install
to install it and everything seemed to be fine. Unfortunately when I tried to execute the program (just tried labelImg &) got an import error:
ImportError: No module named 'resources'
So, I was wondering if I did something wrong or if there something I could do to fix it. My first thought is to provide an absolute import path to resources (and to the following libs imports) but that does not seem the right thing to do. Also it might work for a small project but it's obvious out of reach for a big one.
The git repo seem to imply that I should run it via python, but why then a setup.py exists?
I know I can use the program via python or even install it via PyPI but I am not interested in that.
The labelImg project depends on other libraries to work such as pyqt5-dev-tools and lxml.
If you check their documentation, first you have to install pyqt5-dev-tools:
sudo apt-get install pyqt5-dev-tools
Then install lxml:
sudo pip3 install lxml
After that you have to run the make command in order to build the pyqt5-dev-tools library so that the python code can use it properly (make is used to build executable libraries and programs from source code):
make qt5py3
And finally you can run python3 labelImg.py and use labelImg.

When i share my python project that uses pip, how do i ensure that the user has the pip programs installed?

I have a python project that uses tools/programs from the command line command called pip.
How do i ensure that the user has all the tools i'm using on their computer?
Do i have to include a readme that states you need the following tools in order to properly run the program, or is there some sort of function or module for me that can automatically install the missing features?
Oh, and i'm fairly new to Python, so maybe i just do not understand how pip works. Can i just use os.system("pip install something")? And what if i wanna be not platform specific?
The convention is to include a requirements.txt which contains information on which packages need to be installed. You can read more at the official documentation for pip.
However, since generating that manually can be painful, there are tools like pipreqs which examines your project and generates a requirements.txt file for you by comparing your imports against those which are found through official pip repositories.
Once the requirements.txt file is generated, it can be installed this way: pip install -r requirements.txt.

Python setup.py develop vs install

Two options in setup.py develop and install are confusing me. According to this site, using develop creates a special link to site-packages directory.
People have suggested that I use python setup.py install for a fresh installation and python setup.py develop after any changes have been made to the setup file.
Can anyone shed some light on the usage of these commands?
python setup.py install is used to install (typically third party) packages that you're not going to develop/modify/debug yourself.
For your own stuff, you want to first install your package and then be able to frequently edit the code without having to re-install the package every time — and that is exactly what python setup.py develop does: it installs the package (typically just a source folder) in a way that allows you to conveniently edit your code after it’s installed to the (virtual) environment, and have the changes take effect immediately.
Note: It is highly recommended to use pip install . (regular install) and pip install -e . (developer install) to install packages, as invoking setup.py directly will do the wrong things for many dependencies, such as pull prereleases and incompatible package versions, or make the package hard to uninstall with pip.
Update:
The develop counterpart for the latest python -m build approach is as follows (as per):
From the documentation. The develop will not install the package but it will create a .egg-link in the deployment directory back to the project source code directory.
So it's like installing but instead of copying to the site-packages it adds a symbolic link (the .egg-link acts as a multiplatform symbolic link).
That way you can edit the source code and see the changes directly without having to reinstall every time that you make a little change. This is useful when you are the developer of that project hence the name develop. If you are just installing someone else's package you should use install
Another thing that people may find useful when using the develop method is the --user option to install without sudo. Ex:
python setup.py develop --user
instead of
sudo python setup.py develop

How to "include" third party modules into my python script to make it portable?

I've developed a little script that searches through a wallpaper database online and download the wallpapers, I want to give this script to another person that isn't exactly good with computers and I'm kinda starting with python so I don't know how to include the "imports" of the third party modules in my program so it can be 100% portable, Is there something that can help me do this? or I will have to enter and analyse my third party modules and copy&paste the functions that I use?
Worse thing to do
An easy thing you can do is simply bundle the other modules in with your code. That does not mean that you should copy/paste the functions from the other modules into your code--you should definitely not do that, since you don't know what dependencies you'll be missing. Your directory structure might look like:
/myproject
mycode.py
thirdpartymodule1.py
thirdpartymodule2.py
thirdpartymodule3/
<contents>
Better thing to do
The real best way to do this is include a list of dependencies (usually called requirements.txt) in your Python package that Python's package installer, pip, could use to automatically download. Since that might be a little too complicated, you could give your friend these instructions, assuming Mac or Linux:
Run $ curl http://python-distribute.org/distribute_setup.py | python. This provides you with tools you'll need to install the package manager.
Run $ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python. This installs the package manager.
Give your friend a list of the names of the third-party Python modules you used in your code. For purposes of this example, we'll say you used requests, twisted, and boto.
Your friend should run from command line $ pip install <list of package names>. In our example, that would look like $ pip install requests twisted boto.
Run the Python code! The lines like import boto should then work, since your friend will have the packages installed on their computer.
The easi(er) way:
Start with a clean virtual environment.
Install packages that you need to develop your code.
Once you are done, create a list of requirements for your project.
Send this file (from step 3) to your friend.
Your friend simply does pip install -r thefile.txt to get all the requirements for your application.
Here's an example:
D:\>virtualenv --no-site-packages myproject
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in myproject\Scripts\python.exe
Installing setuptools................done.
Installing pip...................done.
D:\>myproject\Scripts\activate.bat
(myproject) D:\>pip install requests
Downloading/unpacking requests
Downloading requests-0.14.1.tar.gz (523Kb): 523Kb downloaded
Running setup.py egg_info for package requests
warning: no files found matching 'tests\*.'
Installing collected packages: requests
Running setup.py install for requests
warning: no files found matching 'tests\*.'
Successfully installed requests
Cleaning up...
(myproject) D:\>pip freeze > requirements.txt
(myproject) D:\>type requirements.txt
requests==0.14.1

Using pip to install single-file python modules

I'm wondering if there's a way to "install" single-file python modules using pip (i.e. just have pip download the specified version of the file and copy it to site-packages).
I have a Django project that uses several 3rd-party modules which aren't proper distributions (django-thumbs and a couple others) and I want to pip freeze everything so the project can be easily installed elsewhere. I've tried just doing
pip install git+https://github.com/path/to/file.git
(and tried with the -e tag too) but pip complains that there's no setup.py file.
Edit: I should have mentioned - the reason I want to do this is so I can include the required module in a requirements.txt file, to make setting up the project on a new machine or new virtualenv easier.
pip requires a valid setup.py to install a python package. By definition every python package has a setup.py... What you are trying to install isn't a package but rather a single file module... what's wrong with doing something like:
git clone git+https://github.com/path/to/file.git /path/to/python/install/lib
I don't quite understand the logic behind wanting to install something that isn't a package with a package manager...

Categories

Resources