0

I have a problem with my python modules for a project. Here's my tree for my project :

Documents/Projects/projet/
├── code
│   ├── datas
│   │   ├── Lexique383
│   │   │   └── Lexique383.tsv
│   │   └── Lexique383.zip
│   ├── __init__.py
│   ├── __pycache__
│   │   └── __init__.cpython-38.pyc
│   ├── src
│   │   ├── Correcteur.py
│   │   ├── __init__.py
│   │   ├── PoCCode.py
│   │   └── __pycache__
│   │       ├── Correcteur.cpython-38.pyc
│   │       └── __init__.cpython-38.pyc
│   └── ui
│       ├── GUI.py
│       ├── GUI.ui
│       ├── __init__.py
│       ├── Loader.py
│       └── __pycache__
│           └── GUI.cpython-38.pyc

The dependencies are like :

  • GUI.py uses Correcteur.py
  • Loader.py uses GUI.py

I am working on 2 computers for this project, one with PyCharm and one with VSCode. The thing is I used PyCharm to run Loader.py with a configuration where it added content roots and source roots to PYTHONPATH. But when i went back to my other computer and wanted to check if everything worked fine, I had this error :

Traceback (most recent call last):
  File "Documents/Projects/projet/code/ui/Loader.py", line 4, in <module>
    import GUI
  File "Documents/Projects/projet/code/ui/GUI.py", line 13, in <module>
    from code.src import Correcteur
ModuleNotFoundError: No module named 'code.src'; 'code' is not a package

So I tried to add to my syspath "code", "src" and "ui", but I still have the error. Without a doubt I did something that didn't work with sys.path but I can't figure out what. Could you please help me on this matter?

Edit : This is how I added directories to my syspath using pathlib.
Loader.py :

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
import sys
from pathlib import Path
sys.path.extend([
    Path(__file__).parents[1].resolve(),
    Path(__file__).parents[1].resolve() / "src"
])
print(sys.path)
import GUI

And here is how I imported Correcteur in GUI.py :

from code.src import Correcteur
Moa
  • 3
  • 4
  • Thank you for your answer. Unfortunately, I tried both of your choices, but none of them worked for me... I still have an error saying "code" is not a package when I try to do ```from code.src import Correcteur``` – Moa Feb 20 '22 at 02:17
  • Ok, I'll try to do that. But the problem I had with the other thread is that when I go back to VSCode, I can't automatically add content roots et source roots. I'm going to edit my post to add code of how I import and how I add my directories to syspath – Moa Feb 20 '22 at 02:29
  • Sorry to consume your time like that, but I still have problems. I deleted my .idea folder and opened again my project. I checked the two boxes in my run confing in PyCharm and everything works. SI I put my .idea in .gitignore and pushed my changes. But once I went to my other computer i still won't work. Here is my github link to the code if we still can't find an answer : [link to code](https://github.com/cegepmatane/projet-specialise-2022-MoOaAaa/tree/main/code) Edit : I just saw you comment, I added that to test : ```Documents/Projects/projet, Documents/Projects/projet/code```. Still bug – Moa Feb 20 '22 at 03:07
  • @Moa Just to be sure about your environment: Are there any other python files named "code.py" in that package? Or would there be a "code.py" found somewhere else on your path? The fact that the error you're getting says "'code' is not a package" seems to imply that it does **find** something called "code", but it doesn't think it's a package, it thinks it's a module. Related: https://stackoverflow.com/questions/38454852/importerror-with-error-is-not-a-package – ncoish Feb 20 '22 at 03:09
  • Also, @Moa I think that your github project might be private, because that link gives a 404 right now – ncoish Feb 20 '22 at 03:11
  • @bad_coder I have the same problem, when I run my main from Loader.py, I have the same message from Pycharm (when I don't check "add content roots" and "add source roots") and VSCode saying my packages aren't actually packages. – Moa Feb 20 '22 at 03:19
  • @ncoish , I just saw that sorry (the repository is managed by my school..) I changed the visibility of it, so you should be able to access it now. And I don't have any other files named code.py – Moa Feb 20 '22 at 03:19
  • Okay, so the actual error message that you're getting might be masking the issue. It looks like Python has a builtin module named [**code** that it's trying to import](https://docs.python.org/3/library/code.html). Just to see if you get a different error message, can you try changing your package name to something other than "code"? – ncoish Feb 20 '22 at 03:30
  • Okay, I got it working! one sec, I'll post an answer – ncoish Feb 20 '22 at 03:36

1 Answers1

0

Your method of adding to sys.path should work. The main issue is that you are adding pathlib.Path objects to sys.path, which only expects strings.

If you just cast the Path object you're trying to add to a string, then a simplified version of your code should work:

In Loader.py

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
import sys
from pathlib import Path
# Note the call to `str(...)` here
sys.path.insert(1, str(Path(__file__).parents[2].absolute()))
print(sys.path)
import GUI

Additionally, some of the errors that you are getting are because Python has a builtin module named code, which it's importing by mistake, so I would recommend changing your package to have a different name.

Dharman
  • 30,962
  • 25
  • 85
  • 135
ncoish
  • 343
  • 1
  • 13
  • Thanks a bunch, I'm going to test it right now – Moa Feb 20 '22 at 03:43
  • It's working!!! Thanks to both of you for your help, hope one day I can be like you and help people solve "simple" problems like this one! – Moa Feb 20 '22 at 03:50
  • Happy to help! And glad that you're learning. Python packaging tends to be a thorny issue, so great job getting this far! – ncoish Feb 20 '22 at 03:52
  • Thank you. When I asked help from my teachers asking "I have some problems working from PyCharm and VSCode, I think it's from how I import my packages", we laughed together saying how imports can sometimes be hard to deal with. But I'm really glad I created this question, my project will work on near any computers. Again thanks a lot, and have a great night / evening / day – Moa Feb 20 '22 at 03:56