FAIRmat-NFDI / pynxtools

https://fairmat-nfdi.github.io/pynxtools/
Apache License 2.0
12 stars 8 forks source link

Import problems in dev install when in parent directory #196

Closed domna closed 9 months ago

domna commented 9 months ago

If I do

git clone --recurse-submodules https://github.com/FAIRmat-NFDI/pynxtools.git
cd pynxtools
pip install -e .

and in the top directory do

>>> from pynxtools import get_nexus_version
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'get_nexus_version' from 'pynxtools' (unknown location)

I get an import error because python tries to resolve from the folder. This happens if any folder named pynxtools is present. The thing is that this also happens if dataconverter is used in this folder. Maybe we should switch to relative imports because it should avoid this problem? Otherwise, we could just write a note about this in the README. @sherjeelshabih What do you think?

This was reported by @rettigl

sherjeelshabih commented 9 months ago

I couldn't reproduce this. I don't think it should look for folders first anyways. That's odd.

Do you get this error on your machine as well, @domna?

Edit: It only results in an error when we cd out of the cloned repo folder. That is we do the following steps:

git clone --recurse-submodules https://github.com/FAIRmat-NFDI/pynxtools.git
cd pynxtools
pip install -e .
cd ..

Everywhere else it works fine.

domna commented 9 months ago

I did reproduce this in the mpesjupyter container. Maybe they have . in the pythonpath by default? I'll try locally again

Edit: Can also reproduce locally

sherjeelshabih commented 9 months ago

This is, unfortunately, expected behavior. The Python import mechanism will first look for a folder with the package name in the current working directory. If it finds a folder named pynxtools, it will try to import from there. In this case we have the cloned repo folder with the same name, pynxtools. Python tries to import from this folder but the code resides in pynxtools/pynxtools.

Solution

If you are working in a directory containing the repo folder, pynxtools, rename this repo folder to pynxtools2 so your current directory looks like this:

.
├── pynxtools2                    # Renamed repo folder
│   ├── pynxtools          # Actual module code is in here
│   ├── tests
│   └── ...               
└── my_code_imports_pynxtools.py

Then reinstall the package and your import will work.

Steps to reproduce

git clone --recurse-submodules https://github.com/FAIRmat-NFDI/pynxtools.git
cd pynxtools
pip install -e .
cd ..
rettigl commented 9 months ago

I would suggest as solution to change the dev-install instructions to clone into a different folder name, e.g. git clone --recurse-submodules https://github.com/FAIRmat-NFDI/pynxtools.git pynxtools_dev