1

My project has two main folders: sourceCode and lib: Highlighted file tree here

I'm working in \sourceCode\mainFile.ipynb and would like to import a library residing in lib called modifiedLibrary, which has an __init__.py file.

Currently, I'm using a symbolic link for relative-importing the library. The symbolic link is located in \sourceCode and called sym_link with the following content:

../lib/modifiedLibrary/modifiedLibrary

In the project, the library and the symbolic link have the same name.

but when I import in python using

import modifiedLibrary

I receive ModuleNotFoundError: No module named 'modifiedLibrary'

I understand that the same code functions on another device that I do not have access to right now, and I do not seem to find what the issue is.

I successfully included the needed library by:

  1. changing the working directory temporarily to where the library's __init__.py is located,
  2. importing the library
  3. then reverting back to my original directory

but I would like to know what the issue is with the current symbolic link.

Windows 10 / Python 3.7.3 / Jupyter

Relevant Question: Interactive Python - solutions for relative imports

radarhere
  • 929
  • 8
  • 23
  • The relative import syntax you have shown is Python2 only. For Python3, use ``from . import modifiedLibrary`` instead. Note that a notebook would not be run as part of a package (``sourceCode.__main__``) but a top-level script (``__main__``) - you cannot use relative imports at all. – MisterMiyagi Jul 30 '19 at 12:42
  • This may be of relevance: https://stackoverflow.com/a/50392363/5349916 – MisterMiyagi Jul 30 '19 at 12:49
  • @MisterMiyagi using `from . import modifiedLibrary` returns `ImportError: cannot import name 'modifiedLibrary' from '__main__' (unknown location)`. sourceCode only has an ipynb file, the symlink and some dataset. – AghiadHaloul Jul 30 '19 at 13:17
  • As said, you cannot use relative imports at all. You may want to modify ``sys.path`` by preprending ``"\sourceCode"`` - this should allow an absolute import of your package. Properly installing it is much better for reproducibility, though. – MisterMiyagi Jul 30 '19 at 16:14
  • Thanks. I'll have to do more reading on importing in python 3 and what's new from python 2. I'm a bit new to the language. Adding the library to the python path using `sys.path.append('C:/User/.../myProject/lib/modifiedLibrary/modifiedLibrary')` before executing `import modifiedLibrary` is fixing the issue for now. [Basic importing in python 3 tutorial here](https://www.digitalocean.com/community/tutorials/how-to-write-modules-in-python-3) – AghiadHaloul Jul 31 '19 at 13:38

1 Answers1

0

The other solution I found, rather than changing the working directory temporarliy to include a local module, was to add the location of the module on my device to sys.path before importing it:

import sys
sys.path.append('C:/Users/user/myProject/../modifiedLibrary/')

import modifiedLibrary

It doesn't make use of the symbolic link but it seems to do the trick for now. Would be an issue when the code is shared and ran on another device.