1

I have a project such as:

myproject
    setup.py
    -myproject
       -package1
       -package2

I am using a setup.py as:

NAME='myproject'
setup(
    name=NAME,
    version=VERSION,
    description=DESCRIPTION,
    long_description=long_description,
    long_description_content_type='text/markdown',
    author=AUTHOR,
    author_email=EMAIL,
    python_requires=REQUIRES_PYTHON,
    url=URL,
    packages=find_packages(exclude=('tests',)),
    package_data={NAME: ['VERSION']},
    install_requires=require(),
    extras_require={},
    include_package_data=True)

When I install (pip install -e .) this I can access the packages as import myproject.package1. However, I want to change this so I instead import it as import mynewname.package1. In the example above when changing NAME=mynewname and then installing, the packages become no longer visible, and import mynewname gives a ModuleNotFoundError.

I don't want to change the name of the project or structure, just the top level name under which the package is installed. Something like import mynewname.myproject.package1 would also work, but i'm unsure of how to do this.

Thanks

Sam Palmer
  • 1,675
  • 1
  • 25
  • 45
  • 1
    Should be as simple as renaming the directory `myproject/myproject` to `myproject/mynewname`. – sinoroc Sep 11 '20 at 17:32
  • I don't want to rename anything in the project which is the tricky part. I just want to create some sort of installation wrapper or something. The issue is in reality the name of myproject is quite generic, and when installing it I want to install it under a more specific name. – Sam Palmer Sep 11 '20 at 17:41
  • Use `package_dir` then, it will allow you to have different names for the directories in your source and for the actual importable packages (i.e. the directories are renamed during the build). Examples: https://stackoverflow.com/a/63141103/11138259 -- https://stackoverflow.com/a/59662834/11138259 -- https://stackoverflow.com/a/57853378/11138259 – sinoroc Sep 11 '20 at 17:57
  • adding something like : `package_dir={ 'mynewname.myproject': 'myproject'}`, still doesn't work. – Sam Palmer Sep 11 '20 at 18:13
  • There is something I don't understand. Where is located the `setup.py` in the directory tree? Which of the directories in the directory tree are supposed to be the _top-level modules_? – sinoroc Sep 11 '20 at 18:16
  • updated the question, its located at the top level. `myproject'`is the top-level module, but I either want to install it under a different name or with a wrapper module a level above to give `mynewname.myproject` – Sam Palmer Sep 11 '20 at 18:18
  • 1
    Alright then I guess, you need `package_dir={'mynewname': 'myproject'}`. But I believe you will also have to modify the list for `packages`, since `find_packages` will still return untransformed package names such as `myproject.*`. – sinoroc Sep 11 '20 at 18:20
  • 1
    the above approach with transforming the package list works when not in development mode, otherwise in dev mode it still defaults to using the original package name. – Sam Palmer Sep 11 '20 at 18:45
  • 1
    That is true. There is no way around it nowadays as far as I know. – sinoroc Sep 11 '20 at 20:43
  • Is there a solution now, after almost two years? – pratsbhatt Jun 01 '22 at 16:32

0 Answers0