What is the pythonic way to install a single python script - python

I have a git repository with a single python script which I use for some task. I want to create a 'package' for this script and then install it. Previously I was using cmake to do this, but I'm wondering what the pythonic way of doing it is.
I tried using setuptools' console_scripts keyword argument, but that didn't work.
This script should get installed into some ./bin directory.

The pythonic way here would be to create an actual package https://packaging.python.org/en/latest/tutorials/packaging-projects/ and either building something like a .whl and installing that using pip or conda (whichever you use for managing your virtual environment), or publishing the package on PyPI for general use and easy installation on any machine with internet access.
Packaging a project is fairly standard and the linked documentation is official. Getting a package onto PyPI is fairly straightforward as well, although there are many options you can add to make the installation and the accompanying page nicer - there's many guides online on how to publish a package on PyPI, but none of then official documentation, so I suggest just searching for one.

The answer here was to use the scripts keyword instead of console_scripts. my setup.py was then able to properly install the script.

Related

How to download python library in a separate folder?

I want to download python libraries like NumPy, scipy, etc. in a separate folder. I want to include that folder in the python project so that whenever I switch to some other laptop, I don't need to install the libraries again rather I import libraries from that folder. Is there any way?
You can easily install python virtualenv.
Your libraries will be installed in directory created by virtualenv.
https://pypi.org/project/virtualenv/.
Other option, you can also use docker.
I suggest using virtual environment in this case. You could use pipenv so that the project hast exactly the libraries you need for it to run.
You can do it.
for numpy library: https://pypi.org/project/numpy/#files
You can download the files statically from pypi.
I would not recommend you go with this approach. There are several reasons to do that.
There would be a dependency on this kind of library. So you have to keep these dependencies along with the NumPy package.
These libraries are getting updated after some time with some newly added functionality and some bug fixes. So with the time other libraries might not compatible with this library.
Recommended way:
Just create an requirement.txt file that contains all the dependency with its version number.
whenever you want to use your project elsewhere, just install all these libraries with below command.
pip install -r requirement.txt
There are two major way you can install python libs to a separate folder: a virtual environment or a container.
Virtula environment (like venv, pipenv, etc) is good as this is the simplest way to your project's own liblaries set which is not impact any other pythonic script in your system. The downside of this case is thet you really have to set up an environment (including lib install) on every computer you move your script to. This can and should be autimated, of course, but this should be done either way.
The container, in other hand, requires additional resources to handle and to build, build, but it is exactly the box with a specific version of your script along with all libs and binaries it requires. No need to reinstall libs while moving to new laptop/desktop/server/cloud/whatever. For this case I would recommend the Docker/Kubernetes. But it's better to start with Docker.

Python 3.6: Import subdirectorys not working in Pycharm

I have a Pycharm project where I have copied two github projects (download zip and copy paste into project). My problem is that I cannot access the from main, which is located at root.
For some unknown reason from . syntax only shows me files/folders I have created myself.
I'm trying to access build module where there is a class TFNET which I want to import
from darkflow-master.darkflow.net.build import TFNET
Downloading directories like this isn't how you'd typically include external dependencies in a python project.
A more conventional approaches would be ot install the project using pip, it looks like darkflow gives information on how to do this.
Alternatively, just ensure the libraries are in your PYTHONPATH, it looks like pycharm has a way to do this: PyCharm and PYTHONPATH
Reading between the lines, it sounds like you have downloaded two libraries from github, and copied them into your project.
Python needs to know where to find source files.
If you want do it this way, you need to tell your python environment where to find the new sources. Pycharm looks after python environments while you are in python.
Please see https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
but this won't tell python outside of Pycharm where to find your library source.
pip is most likely the answer. pip can install globally or inside python virtual environments, in either case, it puts the library code in a location python is expecting to find it.
On this point, please learn about python virtual environments. These are self-contained python mini-worlds. In a venv, you can run a specific version of python with specific packages. Pycharm works well with them, it is easy to set up virtual envs with pycharm. When you are 'inside' a venv, pip will install into the venv, therefore not touching your system python or the python of any other projects.
Also, pip normally installs from an official repository (pypi) but you can tell it to use a git repository as the source of your install. Normally people who write libraries send their mature versions to pypi so it is unusual to fetch from a git repository, but if you want the very latest version, or if the author has not published the library, it's an option.
Note that pip doesn't work with any arbitrary python code. It must be set up to be seen by pip as a python package.

Lambda package includes pip, setuptools

I followed the AWS guide to prepare a deployment package for my lambda function. The generated zip file is around 9 - 10MB which includes pip, setuptools, pylint. Are they really required?
Here are the commands.
virtualenv v-env
source v-env/bin/activate
pip install xmltodict
pip install requests
deactivate
cd v-env/lib/python3.7/site-packages/
zip -r9 ../../../../function.zip .
Edit: Remove installing boto as it is provided by AWS already
Well, as you may see the guides provide standars and, obviously, guidance for a clean and nice coding or project deployment.
Pylint has a lot of features that help you out while using Python as your programming language, such as checking coding standars, error detection, refactoring help in order to prevend duplicated code, among other tools.
Setuptools is really useful too. Is a development process library designed to facilitate packaging Python projects by enhancing the Python standard library distribution utilities, I encourage you to use it in order to wrap your processes and models in order to have a strong modular project.
And pip is a Package Manager for python packages or modules. You can add, download, delete and a lot more things with it by simple using few words on a line of code. This package manager is useful, you can download wheels, zip's and modules from the internet and easily install them by just using
pip install <module or library name>
So, by answering your question, if you downloaded and installed a package for AWS supported in Python and it installed those libraries I must think those are being used across the modules you want to use.
You can always check the source code in order to be sure.
If the libraries aren't really being used, they aren't necessary since there are several libraries and packages that do what those libraries do.
Hope it helps, happy coding.

How can I import a non standard package in python on mac? This package does not support pip install

I used to work with windows when I install a non basic python package with the cmd, but I dont know how to do it on mac, this packaged cant use pip to install on the terminal
It really depends a lot on the specific package. The options can be different, let me present you some:
You can simply add the source files (if they are not properly packaged) to your project and import them
In the same scenario, if the script is not packaged, you can check if there is a way to install it (read the docs). A common way to do it is by having a script in root called setup.py. Try to see if there is a setup script or read the docs to see if there is a way to install it
Check if the package can not be installed with another general package management (you mentioned you use a mac so check with homebrew)
Another possible package management tool for Python is Anaconda, check that one too
Hope this helps!

PIP install a Python Package without a setup.py file?

I'm trying to figure out how I can install a python package that doesn't have a setup.py file with pip. (package in question is http://code.google.com/p/django-google-analytics/)
Normally I would just checkout the code from the repo and symlink into my site-packages, but I'm trying to get my whole environment frozen into a pip requirements file for easy deployment and testing.
Any ideas?
Fork the repo and add a working setup.py. Then send a pull request to the author.
Oh, it's on Google Code. Well then, file a bug and post a patch.
If the author refuses to make their code into an installable Python distribution (never happened to me), just host your fork somewhere and put that in your requirements file.
You can't. PIP installs Python packages. That's not a Python package. I've heard that the Django community in general doesn't make much packages, which makes things like what you are trying to do tricky. But that could be wrong.
If you want to freeze your environment you might want to look into Buildout. Other options in this case is to use an svn:external.

Categories

Resources