Hodossy Szabolcs
Hodossy Szabolcs

Reputation: 1668

Why current working directory affects install path of setup.py? How to prevent that?

I have created a custom python package following this guide, so I have the following structure:

mypackage/     <-- VCS root
  mypackage/
    submodule1/
    submodule2/
  setup.py

And setup.py contains exactly the same information as in the guide:

from setuptools import setup, find_packages

setup(name='mypackage',
      version='0.1',
      description='desc',
      url='vcs_url',
      author='Hodossy, Szabolcs',
      author_email='[email protected]',
      license='MIT',
      packages=find_packages(),
      install_requires=[
          # deps
      ],
      zip_safe=False)

I have noticed if I go into the folder where setup.py is, and then call python setup.py install in a virtual environment, in site-packages the following structure is installed:

.../site-packages/mypackage-0.1-py3.6.egg/mypackage/
  submodule1/
  submodule2/

but if I call it from one folder up like python mypackage/setup.py install, then the structure is the following:

.../site-packages/mypackage-0.1-py3.6.egg/mypackage/
  mypackage/
    submodule1/
    submodule2/

This later one ruins all imports from my module, as the path is different for the submodules.

Could you explain what is happening here and how to prevent that kind of behaviour?

This is experienced with Python 3.6 on both Windows and Linux.

Upvotes: 0

Views: 3372

Answers (1)

allo
allo

Reputation: 4236

Your setup.py does not contain any paths, but seems to only find the files via find_packages. So of course it depends from where you run it. The setup.py isn't strictly tied to its location. Of course you could do things like chdir to the basename of the setup file path in sys.argv[0], but that's rather ugly.

The question is, WHY do you want to build it that way? It looks more like you would want a structure like

mypackage-source
  mypackage
    submodule1
    submodule2
  setup.py

And then execute setup.py from the work directory. If you want to be able to run it from anywhere, the better workaround would be to put a shellscript next to it, like

#!/bin/sh
cd ``basename $0``
python setup.py $@

which separates the task of changing to the right directory (here I assume the directory with setup.py in the workdir) from running setup.py

Upvotes: 1

Related Questions