0

I have a question regarding simple imports that I cannot get my head around.

Take a look at the attached screenshot to see my project layout. enter image description here

The file somefile.py imports the class SayHello from a file called someclass.py and calls it. someotherfile.py does the exact same thing. They both use from someclass import SayHello.

In Pycharm both files run. However, From the command line or from VSCode somefile.py runs, but someotherfile.py errors out with the following error:

ModuleNotFound: No module named 'someclass'.

I believe it has something to do with PYTHONPATH/environment variables or something like that, but every explanation I have read has confused me thus far (Even this one which I thought was going to set me strait Relative imports for the billionth time).

Can someone explain in simple terms what is happening here? What is Pycharm doing by default that other editors are not such that my imported modules are found? How can I make someotherfile.py work in VSCode without modifying it?

Thanks in advance!

Kevin
  • 391
  • 3
  • 6
  • 22

2 Answers2

1

Pycharm adds your project directory into python paths by default. See configuration of "Pycharm run" that you execute and you shall see few checkboxes like enter image description here
If those checked Pycharm creates PYTHONPATH environment variable for you that instructs Python where to look for someclass module.

You will have to configure VSCode to define PYTHONPATH environemnt variable for python command you run and include your root project directory path on it.

mojeto
  • 536
  • 4
  • 12
  • Thanks for the help. I took a look at some other guides namely https://k0nze.dev/posts/python-relative-imports-vscode/ and the main documentation at https://code.visualstudio.com/docs/python/environments Still no luck. I am a bit confused as to why this is so complex. There are some sources that say to do it one way, and other sources say to do it some other way. Yet, there are other sources that say that VSCode is bugged in terms or using Environment Variables. It's so strange that PyCharm "just works". Thanks I'll keep looking for more resources. – Kevin Mar 17 '22 at 11:17
  • If you run `python onedeep/twodeep/someotherfile.py` in the terminal it will fail because what is happening is "similar" to `cd onedeep/twodeep/ && python someotherfile.py` - Python has no idea where to find `someclass` module - it won't search two levels up for it. As @mojeto said - PyCharm implicitly adds your project root to `sys.path`, hence Python will be able to find `someclass` module there. To make it work in the terminal you can do e.g. `PYTHONPATH=. python onedeep/twodeep/someotherfile.py`. – Pavel Karateev Mar 18 '22 at 09:50
  • Thanks @PavelKarateev. That is very helpful for getting it to run in the console. Actually, I figured out how to resolve my issue which strangely enough was very simple. I'll post an answer in a bit. – Kevin Mar 18 '22 at 11:22
0

TLDR: Mess with the line starting with ./automated pointing it to various directories in your project until it works haha.

Long rambling answer: Alright now that I am not in a frenzy from trying to figure this out and it has been a day, I feel like I can make a conherint response (lets see if that is true).

So my original answer was an attempt to simplify my problem into what I thought was the issue due to a ModuleNotFound error I was getting. I had been trying to get unittests in Python inside of Visual Studio code to work (hint hint just use Pycharm it just works out of the box in there), but the integrated testing feature or whatever could not find my tests giving ModuleNotFound as the reason.

The solution to my problem actually just concerned the line ./automated-software-testsing-with-python/blog.

In the below screenshot the relevant part is ./automated-software-testing-with-python/blog. enter image description here

This is it when it is correctly configured (My tests work Woo hoo!)

Now you can go ahead and read the official documentation for what this line is doing, but I am convinced that the documentation is wrong. Due to how my code is structured and how my files are named, what the documentation says its looking for definitely does not exist. But that is another can of worms...

I actually got it to work by accident. Originally when you go though the wizard to set up what's in that screenshot above, it defaulted to ./automated-software-testing-with-python which did not work. I then manually edited it to something else that was not the below screenshot hoping to get it to work.

It was only when I pointed it to that blog directory on accident (thinking I was in a different file due to trying to debug this for hours and hours in a blind rage) that it ended up working.

I did a bunch of PYTHONPATH manipulation and Environment Variable mumbo jumbo, and I originally thought that that had an effect, but when I cloned my repot to my other machine (which did not have any of that Environment Variable PYTHONPATH stuff going on) it worked too (again, provided that the line in question pointed to blog.

Ok hopefully that might help somone someday, so I'll end it there. Not to end on a bitter sounding zinger, but I will never cease be amazed by how doing such simple things such as configuring the most basic unit test can be so difficult in our profession haha. Well now I can start working. Thanks for the help to those who answered!

Kevin
  • 391
  • 3
  • 6
  • 22