0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

What if, say, I want to take my code to another computer? Then, this whole setup has to be done again?
@user13985 yes. Usual practice in this case is to package the code in some way and distribute it either as a part of the user code (include content of Deep Leaning into the project) or to publish it as a package and then install it with pip. In the latter case, you don't have to publish a PyPI package, pip can install from github dicrectly: pip install <github_url>
That's tedious, nobody ever complained about it? My friend lost Kaggle competition because he used PyCharm and the judge was not! The judge can't read in his package. He supposed to write import os and all that.
@user13985 there is a good reason it was designed so, and I think that benefits of this solution outweigh the inconvenience. A complete explanation won't fit into the comment limit; basically, it was done so to be isolate from environment and be have flexible dependencies management. This is why in Python can have isolated environments like Conda but R will never do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.