Creating a python package with dependency - python

I created a python package with a dependency that fails to install using pip due to some missing wheels and non pure-pythonic code (needs a Microsoft Visuals Compiler). Other dependencies are installed normally using pip.
the problematic dependency (geopandas->pyproj) is only used in part of my package so I was wondering if I could allow the user to install my package using pip with all functionality except the functions that require the dependency. If the user wants to use the functions in the package that requires the dependency one could simply install it in addition to my package allowing for more flexibility (make use of pip, conda, compile etc.):
pip install mypackage
conda install dependency
and then
import mypackage
import dependency
bar = mypackage.function_that_requires_dependency(foo)
If the user cannot install the dependency, it can still use all parts of my package that do not rely on it.
pip install mypackage
and then
import mypackage
bar = mypackage.function_that_does_not_require_dependency(foo)
Is there a way to accomplish this? I currently have all my imports at the beginning of my init.py file.
package github
package PyPi

Related

python and pip: dependency is never upgraded with PEP508 URL requrements

PEP508 allows specifying a URL for a dependency, in particular a VCS. This is most useful for private packages that are not on pypi. If I have a package whose setup.py looks like:
from setuptools import setup
setup(name='foo',
install_requires=['bar # git+ssh://git#github.com/me/bar#1.2.3']
)
Then when I say pip install foo, it will download and install bar from the github repo. But if I later want to install a new version of foo, (pip install --upgrade foo), which has an updated bar dependency (e.g. tag 2.3.4), pip says that the dependency is already satisfied.
Is there a way to encode version information or something that will force pip to recognize that the dependency is NOT being met? I know I can give pip the --upgrade-strategy eager option, but that would affect ALL dependencies recursively and is too heavy-handed.
This related question PEP508: why either version requirement or URL but not both? asks about not being able to specify a version, but doesn't answer why pip doesn't get the URL when asked to upgrade.

rasa core installation existing package found

I am downloading rasa core and the NLU. But installing rasa core is presenting a frustrating error I do not understand.
pip install rasa_core
Results in an error
Installing collected packages: pyparsing, kiwisolver, matplotlib, rasa-nlu, graphviz, redis, fakeredis, decorator, networkx, fbmessenger, click, itsdangerous, flask, jsonpickle, h5py, Keras, tzlocal, apscheduler, websocket-client, slackclient, python-telegram-bot, ply, pandoc, packaging, snowballstemmer, alabaster, sphinxcontrib-websupport, babel, imagesize, sphinx, nbsphinx, monotonic, humanfriendly, coloredlogs, docopt, pykwalify, ConfigArgParse, ruamel.ordereddict, ruamel.yaml, rasa-core
Found existing installation: pyparsing 1.5.6
Cannot uninstall 'pyparsing'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
I can not find any information on this error or a work around?
My python version is 2.7 but I don't think that would make a difference.
I also don't under stand why it would want to uninstall the package to then re install it (Upgrade?).
Either remove the package manually or overwrite it:
pip install pyparsing --ignore-installed
If you want to uninstall python package which is part of distutils, you can manually remove the folder from 'site-packages' folder. If it is Anaconda distribution, it will be in following folder. I suggest to cut the folder and paste it somewhere else for backup purpose.
C:\Users\<WindowsUser>\AppData\Local\Continuum\Anaconda3\Lib\site-packages
Following 2 items needs to be removed.
Folder [package version no.]
File - [package>.egg-info]

Python install sub-package from package

it is possible to install some special sub-package from package?
For example, I want to create package with slack, datadog, sentry plugins (wrappers). But I want to allow user what he wants to install.
Like:
pip install super_plugins --plugins slack, datadog
Can it be done without separating all plugins to different packages?
Actually, It is quite simple. This is called Packaging namespace packages.
https://packaging.python.org/guides/packaging-namespace-packages/
All you need is to separate all packages to sub - packages and after install it with a namespace.
# for all packages
pip install super_plugins
# for specific
pip install super_plugins.slack super_plugins.datadog

Having a python module, install its own dependencies before running

A lot of software packages require users of the system to install a set of dependencies first before they can use the software. (This is a general question not specific to python, but I'll speak in context of python.). Can't we make the python module install its own dependencies before executing the code if the dependencies are not already installed. This should be doable with the help of system or subprocess calls but I rarely see people doing this.
For example lets say lib is a python library that needs to be used in the python file main.py:
import os
try:
import lib
except:
os.system('pip install pdir')
import lib
# Can make use of lib now
Is there anything potentially wrong with this approach? Could doing something like this cause problems for big projects?
Note: The advantage here is that a user using the file does not have to install the dependencies separately, he can simply run python main.py. And the second thing that I realize is that such approach makes sense only when virtualenv is being used.
You should never do this - pip doesn't have dependency resolution so there's no guarantee that you'll get a certain version. Dependencies should be installed using setup.py, requirements.txt or a different approach.
You also shouldn't need user permissions or sudo to install the packages just for running the code. The user should be aware of the packages that are needed for installing your package as they may come from PyPI or the OS's package system or an internal company PyPI mirror - and silently installing dependencies is not a good idea in that case.
You could always consider using a more advanced print statement to inform the user.
try:
import ConfigParser
except ImportError as err:
print '\n'.join([i + ''.join(str(err).split(' ')[-1:]) for i in ['$ pip install ', '$ easy_install ']])
You can recommend them installing the missing package this way.
$ pip install ConfigParser
$ easy_install ConfigParser
Additionally you could consider having a ImportError be reason to recommend the user to run their ./setup.py or ./INSTALL instead of just the missing module.

Deploy a sub package with distutils and pip

I am wanting to create a suite of interrelated packages in Python. I would like them all to be under the same package but installable as separate components.
So, for example, installing the base package would provide the mypackage but there would be nothing in mypackage.subpackage until I install it separately.
Is this possible with distutils and pip?
What you are looking for is called "namespace packages", see this SO question

Categories

Resources