user3685918
user3685918

Reputation: 427

Are different between 'RUN' and 'RUN (full) selection' in Spyder?

I have a source code consists of many custom modules.

In the first few lines, there are import words such as...

import custom_module_1
import custom_module_2
import custom_module_3
....

When I run(shortcut is F9) this code with partial or full selection, I found ModuleNotFoundError: custom_module_1.

However, when I run(F5) code, I works well runfile('C:/Users/user/Desktop/test.py', wdir='C:/Users/user/Desktop')

I am so confused because I thought 'run(F5)' and 'full selection run(F9)' are same.

However the results are very different.

Is there any different between 'run(F5)' and 'full selection run(F9)' in Spyder?

Upvotes: 1

Views: 452

Answers (1)

Carlos Cordoba
Carlos Cordoba

Reputation: 34186

(Spyder maintainer here) The difference is the following:

  • Run selection takes the code you've selected in the Editor, pastes it in the console and runs it.
  • Run file is similar to executing python myfile.py, but before doing that it changes the directory your code will be run to the one your file is placed in. It'll also run it in a clean namespace so it's not affected by the variables currently defined in the console. Especially due to this last feature, you should avoid using Run selection as much as possible.

In your case I think the problem is that Run selection doesn't change directories, so Python can't find the modules you have next to test.py.

Upvotes: 1

Related Questions