user13985
user13985

Reputation: 221

Why PyCharm does not load file/module from specified directory?

I want to load a module/file called lr_utils inside folder "Deep Learning".

I am relatively new to Python and PyCharm. In my other languages, I just change the directory, and load the package. But, that does not work in PyCharm.

import os
> os.chdir("/Users/Desktop/Deep Learning")
> os.getcwd()
'/Users/Desktop/Deep Learning'

The directory is changed. Let's import.

from lr_utils import load_dataset

Now, I get this error message:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 20, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'lr_utils'

I've been reading that you can go to PyCharm interpreter and manually add the path. But that's too tedious! What If I have multiple paths? What if I want to send me a code? What if I switch to different computer?

Bottom line: I just want to change directory on the fly.

Upvotes: 0

Views: 1217

Answers (1)

Marat
Marat

Reputation: 15738

The short answer: you can't.

Changing directory doesn't have anything to do with module load. What matters, however, is python path - a set of directories where Python is going to look for modules. It includes the directory Python was started from, plus a set of folders containing Python modules.

While there is no clean solution, there are couple dirty workarounds. For example, you can set PYTHONPATH to include the desired folder as a package folder, or create a symlink from the project folder.

Upvotes: 1

Related Questions