Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

package_dir in setup.py not working as expected

I'm trying to let users write code as a python module (folder with __init__.py defined) under whatever folder name they see fit. After that I want to install that module as a python package but define the import name myself.

The folder structure would be like this:

project_name/
    user_defined_name/
        __init__.py
        ...
    setup.py

According to this I should be able to add this to my setup.py to get it working:

setuptools.setup(
    package_dir={'my_defined_name': 'user_defined_name'},
    packages=['user_defined_name']
)

But the only way that I was able to access the code was by using import user_defined_name. I tried installing the package without -e but that gave the same result. Leaving the packages=['..'] out of the setup functions also did not change the result.

My question is kind of the same as this one and there the only answers seem to be to change folder names and that is something that I would like to avoid. That question mentioned that it might be due to a problem in setuptools but that seemed fixed 3 years ago.

like image 839
G. Ballegeer Avatar asked Nov 14 '20 20:11

G. Ballegeer


People also ask

What is Package_dir in setup py?

package_dir = {'': 'lib'} in your setup script. The keys to this dictionary are package names, and an empty package name stands for the root package. The values are directory names relative to your distribution root.

Is setup py deprecated?

You may still find advice all over the place that involves invoking setup.py directly, but unfortunately this is no longer good advice because as of the last few years all direct invocations of setup.py are effectively deprecated in favor of invocations via purpose-built and/or standards-based CLI tools like pip, build ...

What is Install_requires in setup py?

install_requires is a section within the setup.py file in which you need to input a list of the minimum dependencies needed for a project to run correctly on the target operating system (such as ubuntu). When pip runs setup.py, it will install all of the dependencies listed in install_requires.


1 Answers

In short, it looks like you need something like that in your setup.py:

setuptools.setup(
    package_dir={
        'my_defined_name': 'user_defined_name',
    },
    packages=[
        'my_defined_name',
    ],
)

as Ilia Novoselov said in a comment to your question.

This should work, if you package and install the project normally. You would be able to import my_defined_name.

Although, note that as far as I can tell, this will not work if you use an editable installation (python setup.py develop or python -m pip install --editable .). It will be impossible to import my_defined_name, but you would be able to import user_defined_name, which is not what you want.

like image 177
sinoroc Avatar answered Nov 05 '22 18:11

sinoroc