1

I have seen here, here and here but still not sure how to fix it.

I am doing my dev work on my Mac using PyCharm and everything works and imports.

When I take this project directory to FreeBSD jail. It fails to load internal directory code. and gives the following error:

Traceback (most recent call last):
  File "project/src/my_script.py", line 3, in <module>
    from src.auth import MyClient
ModuleNotFoundError: No module named 'src'

I am trying to execute the file my_script.py which imports src.auth.py

My Directory structure looks like this:

project
  /src
   __init__.py   
   my_script.py
   auth.py
__init__.py
.env

both __init.py__ are empty files. Any help would be appreciated. thanks

Shery
  • 1,808
  • 5
  • 27
  • 51
  • Pycharm always sets the Python path to the project's base directory, which is why you need to import your code with the actual path to the files, e.g. `from src. ...`. This is not the default behavior for other programs/ways to run Python. Try removing `src.` from all your import statements. – jfaccioni Apr 13 '20 at 12:20
  • thanks. So if I need to import the `auth.py`. What's the best way to import this? @jfaccioni – Shery Apr 13 '20 at 12:23
  • Simply writing `from auth import ...` should suffice (assuming the folder structure you posted above). – jfaccioni Apr 13 '20 at 12:24
  • when I do from `from auth import ...` pycharm gives error: `Unresolved reference 'auth' – Shery Apr 13 '20 at 12:49
  • Yes because as I said, PyCharm by default sets the working directory to your project root (the folder called `project`), not to folder `src`, where your code actually lies. In PyCharm, you need to change the working directory to `/path/to/your/project/src` (in the execution bar top right corner > "Edit Configurations..."), and mark the folder `src` as the source code folder (right click it on the project viewer > "Mark Directory as"). – jfaccioni Apr 13 '20 at 12:59

0 Answers0