Import package from PYPI - python

I use the "pip install xxx" from PYPI (https://pypi.python.org/pypi). Then I type "import xxx", it can import package without any problem.
However, when I uploaded my package to PYPI, Then I type "import xxx", it cannot import package. It said "ImportError, no module named xxx".
I think it is because the package is not my current directory? If yes, how should I do to avoid this problem when I uploaded my package to PYPI? Thanks.

Your package does not contain any (valid) Python packages. Python package by definition has to have a __init__.py. Just put an empty __init__.py inside the mypackagemx3292016 folder.
I would however suggest not to use a package but rather just a single module. A package works good when you need to group multiple modules together. A simple example from distutils docs shows how to list individual modules.
In terms of installation you need to do exactly the same as with any other package:
pip install mypackagemx3292016
If you want to avoid the hassle of having to do this every time you upload a new version to pypi, you can symlink the local copy:
pip install -e /path/to/mypackagemx3292016
If that does not work there is probably a problem with your setup.py.

Related

How do I specify the name of a Python package import?

I have a python package fmdt-python that anyone can install with pip install fmdt-python. I want to configure this package so that I can call import fmdt anywhere. Despite my best efforts, after successfully installing fmdt-python python can't actually find the package fmdt. How do I configure the project.toml of my pypi project fmdt-python to be imported as fmdt in python?
For reference, the pypi package ffmpeg-python is imported in python as ffmpeg We can inspect the local path pip uses to install packages to see that there is a long versioned name of the package alongside a shorter name used in the import statement:
but for my package fmdt-python pip only installs the directory with the long name:
I would like to configure my package so that pip installs the proper fmdt folder alongside fmdt_python-0.0.12.dist-info.
I am using hatchling as the build system and use a pyproject.toml file to configure this package. For reference, here's the github of the package and this is the pypi index.
The "problem" with my directory structure is that I had a python package fmdt in which I placed all of the configuration files like pyproject.toml, LICENCE, setup.py, etc.
Rearranging the structure to:
with the config files outside of the fmdt folder, I was able to configure my build to make the fmdt package available for import when downloading the pypi distribution fmdt-python

Calling pip package files from within pip package? (`No module named 'src.snnalgorithms'`) [duplicate]

This question already has answers here:
What is the use case for `pip install -e`?
(3 answers)
Closed 4 months ago.
Whilst in some_other_package, I am importing files from the snnalgorithms pip package. I received the error:
No module named 'src.snnalgorithms'. This is a valid error because that src.snnalgorithms file does not exist in the some_other_package project from which I was calling the pip package.
As a solution, I can make all the imports in the snn.algorithms pip package, relative to itself. Instead of:
from src.snnalgorithms.population.DUMMY import DUMMY
One could write:
from snnalgorithms.population.DUMMY import DUMMY
However, that implies that each time I want to run the code to briefly verify a minor change, or run the tests after a change, I will have to:
Upload the changes into the pip package.
Re-install the pip package locally to reflect the changes.
This significantly slows down development. Hence I was wondering are there more efficient solutions for this?
You can use editable mode for development mode
pip install -e . # Install package locally
From pip documentation:
Editable installs allow you to install your project without copying any files. Instead, the files in the development directory are added to Python’s import path. This approach is well suited for development and is also known as a “development installation”.
With an editable install, you only need to perform a re-installation if you change the project metadata (eg: version, what scripts need to be generated etc). You will still need to run build commands when you need to perform a compilation for non-Python code in the project (eg: C extensions).
Example
If you have project: some_other_package from which you call pip package snnalgorithms you can:
cd snnalgorithms
pip install -e .
cd ..
cd some_other_package
python -m src.some_other_package
Assuming you use the same conda environment for both packages, both packages will then be able to use your newest changes that have not even been published to pypi.org yet.

Python3. Setuptools. Adding a local package to an assembly

There is a locally built package (eg main-0.1.tar.gz). There is another package (for example base-0.1) that requires main-0.1 as a dependency.
It is necessary that during the subsequent installation of the base-0.1 package, the main-0.1 package is also installed.
Those. You can specify only packages with PyPI in install_requires, but local adding packages to the assembly is not clear how.
You can add the package main-0.1.tag.gz to the base-0.1 archive using MANIFEST.in (include main-0.1.tag.gz). But further dependency_links, for example, does not work correctly.
How do I add a local package to the build of another package and then install it along with another package, as if it were pulled from PyPI?
You might want to look at:
PEP 440 ("File URLs")
PEP 508
import setuptools
setuptools.setup(
# [...]
install_requires = [
'main # file:///path/to/main-0.1.tar.gz'
# [...]
],
)
Alternatively (probably better actually), use some combination of pip install options:
pip install --no-index --find-links '/path/to/distributions' main base
Reference:
https://pip.pypa.io/en/stable/user_guide/#installing-from-local-packages
Found a rough solution. I don't know how much it is for Feng Shui, but it works.
Add include main-0.1.tar.gz to MANIFEST.in
In setup.py, at the end of the file (after calling setup ()), add:
if 'sdist' not in sys.argv[1]:
os.system('pip install main-0.1.tar.gz')
The condition may be different if, for example, sdist is not used for building (python setup.py sdist). The main thing is to somehow determine that this is running setup for assembly, and not for installation (pip install base-0.1.tar.gz in the future).
In this case, we copy the local dependent package into the archive of the package being built, and it is distributed, accordingly, along with it. And installed the same way.

How to upgrade/uninstall distutils packages (PyYAML) in windows OS

I am working in WIN10 , with python 2.7.15
I am try to install package, during the installation process I received the following error .
Cannot uninstall 'PyYAML'. 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 try to uninstall with pip (18.1) command and I received the same error.
pip uninstall PyYAML
How I can uninstall/upgrade distutils packge in win10 OS.
Base distutils functionality doesn't leave any information about which files belong to a package -- thus it cannot be reliably uninstalled. That's what the message is telling you. Moreover, it doesn't have dependency metadata, so it can't be "upgraded" reliably, either. All those features are additions by setuptools (and some by wheel and pip itself).
This can happen if you installed the package directly from source with setup.py install if setup.py is distutils- rather than setuptools-based. Or if you installed it manually from some types of packages by copying/extracting files.
Unless the way you installed it provides an own uninstaller, you'll have to manually figure out which files belong to the package and delete them from Python directories.
Usually, these are:
site-packages\<package_name>* directories and/or
site-packages\<package_name>*.py for standalone modules
optionally, a site-packages\<package_name>.pth file
Generally, look for anything that bears the package's name on it.
If you can build the same package from source, you can use the build process to get a hint: build a binaly package that you can look into (e.g. setup.py bdist_wheel -- .whl is a ZIP archive) and see what files it has in it.

Using setuptools to install files to arbitrary locations

Is there a way to install files to arbitrary locations with setuptools? I've used Data Files with setuptools before, but those are typically installed inside the package directory. I need to install a plugin file that will be located in the install directory of another application.
It seems that setuptools has purposely made it difficult to install files outside of the package directory.
I instead included the plugin files as package data and used the Entry Points feature of setuptools to expose the install/uninstall functions for the plugin files I wanted to distribute.
setup(
...
entry_points={
'console_scripts': [
'mypackage_install_plugins = mypackage:install_plugins',
'mypackage_uninstall_plugins = mypackage:uninstall_plugins',
],
}
)
I just added an additional step to the installation instructions to run the following command after installing the python package:
$> mypackage_install_plugins
The data_files attribute will allow you to specify full paths.
You could also do some shutil.copy magic in your setup.py, except don't.
Check out this answer:
Execute a Python script post install using distutils / setuptools
which shows how to add an arbitrary install script (python, shell, whatever) that runs at the end of the install. It'll run whther you use "setup.py install" directly, or a package manager like "pip install". With this, you can add any files you want, anywhere you want.
Unfortunately, I feel Brendan's pain - setuptools, not being a full package manager itself, does not handle the uninstall. Therefore, there's no way to have an uninstall hook to reverse what you did in the post-install script.

Categories