7

I have the following arborescence:

- root_folder <--- I'm here
    - folder
        - setup.py
        - myModule

When I run python setup.py install from folder, myModule is installed properly and I can do import myModule.

Howerver, when I run python folder/setup.py install from root_folder, import myModule fails, I have to call import folder.myModule.

How can I call the setup.py script from another folder but keep the root folder to be the folder containing the setup.py file?

BiBi
  • 7,418
  • 5
  • 43
  • 69
  • Hmm, AFAIK, `setup.py` expects to be called from the current directory. Calling it from a different directort may have unwanted and unpredictable effects. – Serge Ballesta Dec 20 '18 at 15:13
  • Maybe related to your problem: https://stackoverflow.com/questions/7505988/importing-from-a-relative-path-in-python – MaLarsson Dec 20 '18 at 15:14
  • If you always want the root folder to be `folder` then you may mark the folder as [PYTHONPATH](https://stackoverflow.com/questions/19917492/how-to-use-pythonpath) – Soumendra Dec 20 '18 at 15:22
  • @Soumendra I'm installing multiple packages from different folders, so I'd prefer not setting the `PYTHONPATH` to `folder` here. – BiBi Dec 20 '18 at 15:26
  • Okay, I think you will be calling `setup.py` only from the `root_folder` and never directly run the standalone `setup.py` file, so what's wrong in `import folder.myModule` ? – Soumendra Dec 20 '18 at 15:28
  • Nothing is wrong with `folder.myModule`, I simply want another behaviour. – BiBi Dec 20 '18 at 15:30
  • did you find solution at the end? – quantum231 Jun 22 '21 at 17:40

1 Answers1

-1

Yes, you can run a setup.py in a different directory by executing Python in a subprocess.

For example, if the folder in which you want to run the setup.py is C:\Program Files\foo, then you can use:

$ python -c "import subprocess,os; os.chdir('C:\Program Files\foo'); subprocess.call(['python','setup.py','install'])"
Paolo
  • 21,270
  • 6
  • 38
  • 69