-3

Basically, I can only reference my other files as modules when they are in a very specific location:

C:\Users\Dave\Desktop\Programming\Python. 

If I want to create a new folder for a large project with multiple modules, say

C:\Users\Dave\Desktop\Programming\Python\Project1,

I can no longer import any modules and keep getting a ModuleNotFoundError. I've looked into it and it seems I need to add that folder to the Python Path somehow, but I couldn't find any answers on how to do it. My computer runs on Windows 10 if that matters.

Louwil
  • 11
  • 1
  • it looks like you are working with two different user profiles - not sure if that sounds right. – rv.kvetch Sep 16 '22 at 13:15
  • 1
    @rv.kvetch I edited the first one just in case but forgot to do the same with the second one, so that's not what caused the problem – Louwil Sep 16 '22 at 13:23
  • Does this answer your question? [Permanently add a directory to PYTHONPATH?](https://stackoverflow.com/questions/3402168/permanently-add-a-directory-to-pythonpath) – gre_gor Sep 16 '22 at 13:24
  • @gre_gor It looks like that's what I need but I do not understand the answers there. My system does not use bash so the first answer does not apply, the second one doesn't work, and I just don't understand the rest – Louwil Sep 16 '22 at 13:31
  • What happened when you tried the second one? – Pranav Hosangadi Sep 16 '22 at 13:39
  • @PranavHosangadi It didn't return any errors, or anything like that. It just didn't change anything or fix the issue. – Louwil Sep 16 '22 at 13:42

1 Answers1

0

I think the immediate solution to your problem/the answer to your question would be to use sys.path.append() at the top of your script.

import sys
sys.path.append("<ABSOLUTE/PATH/TO/YOUR/CUSTOM/MODULES/FOLDER>")

import custom_module

This isn't an ideal solution for any kind of prod use, however. Based on what you wrote in your question, this may not be what you're looking for. More info might help to craft a more stable solution:

  • Are you using virtual environments when running python? Do you just run python script.py or is there a specific version of python you're using that's installed elsewhere than the common C:\Program Files\Python?

  • When you say that you work on a project with multiple modules, does that mean there are custom modules that you/someone wrote or is it just that that project uses non-standard library modules, i.e. you had to pip install them?

  • Can we get an example of the code you're running and the folder structure of your project/where the modules are that you need?

Huessy
  • 111
  • 8
  • Well, sys.path.append() didn't work. As for your questions, I use Pycharm to write and run my Programs. When I say I work on a project with multiple modules, I mean that there are multiple custom modules that I wrote. Library modules work just fine. – Louwil Sep 17 '22 at 09:25
  • Nevermind, I got it to work. All I needed to do was write import .module instead of just import module. – Louwil Sep 17 '22 at 10:05