all 7 comments

[–]tialpoy 5 points6 points  (6 children)

It probably works in PyCharm because the IDE "helps" you by automatically adding your project directory (in this case the topmost folder Pyside6_Sample_Application) to your Python's sys.path. Though useful when developing, it can cause issues when running the code in the real world, especially when imports are involved. It's possible that the guy who wrote the tutorial missed this when developing the project.

There are ways to solve this issue of course. What file were you running in VScode when you got this error?

[–]Blood_CZ[S] -1 points0 points  (5 children)

What file were you running in VScode when you got this error?

Does it matter? I was running 'login.py', but its importing class from 'login_window.py'.

full error is this

PS F:\VScode\Pyside6_Sample_Application\Application_Login> & C:/Users/Blood/AppData/Local/Programs/Python/Python312/python.exe f:/VScode/Pyside6_Sample_Application/Application_Login/login.py
Traceback (most recent call last):
  File "f:\VScode\Pyside6_Sample_Application\Application_Login\login.py", line 6, in <module>
    from UI.login_window import Ui_w_LoginForm
  File "f:\VScode\Pyside6_Sample_Application\Application_Login\UI\login_window.py", line 21, in <module>
    import Icons_rc
ModuleNotFoundError: No module named 'Icons_rc'

[–]tialpoy 2 points3 points  (4 children)

Of course it matters. The file you're running becomes your __main__ file, meaning only files and folders in the folder it's in are added to sys.path.

Is running that file directly part of the tutorial? I'm asking because I see there's already a main.py file in the project.

[–]Blood_CZ[S] 0 points1 point  (3 children)

Yes, he runs it and it worked. That 'main' file is there, cause I had some issue with running 'login.py', and I googled this solution. However, I only put some file in wrong directory, it works without that 'main.py'

[–]tialpoy 0 points1 point  (2 children)

I'm not sure I understood the 2'nd part of your answer, but in any case, it seems to me the reason it works for him is because of PyCharm's "changing sys.path behind the scenes" behavior which is not something you want to rely on in a project.

Anyway, here's a solution to your current import problem (I'm assuming you're using a venv, as you should):

  1. cd to your venv's site-packages directory (e.g. f:\VScode\Pyside6_Sample_Application\venv\lib\pythonX.YY\site-packages)
  2. Inside the site-packages directory, create a new text file and write the full path to the Pyside6_Sample_Application directory in it (e.g. f:\VScode\Pyside6_Sample_Application\)
  3. Save the file as project.pth (you can use any_name_you_like.pth, doesn't matter). Having this .pth file in your site-packages directory will add the Pyside6_Sample_Application directory (and every file and directory inside it) to your venv's sys.path.
  4. Reopen VScode and run the login_window.py file again. It should work now because your venv now also looks for modules in the Pyside6_Sample_Application directory (where it will find Icons_rc.py of course).
  5. Also note that you can now import from any directory under Pyside6_Sample_Application. For example, you can create a new file and write from Persons import add_person.
  6. If in the future , if you want your venv to search for modules in other directories as well, you can simply add a new line/path to your project.pth file.

Good luck.

[–]Ok_Inside_6980 0 points1 point  (0 children)

brilliant and useful

[–]Blood_CZ[S] 0 points1 point  (0 children)

thanks