Repository: spyder-ide/spyder

Issue:
Startup run code of IPython is not working when using projects
### What steps will reproduce the problem?

<!--- You can use Markdown here --->

Hello.
I have several types of spyder projects.
Since the commands required for each project are different, I created an "import_in_console.py" file inside the project folder and set it up as follows. 
![config](https://user-images.githubusercontent.com/22970578/140517201-1f991ed3-14a8-4391-a8bd-8abe7f47675d.png)

After merging PR #16324, the commands in import_in_console.py are not loaded when spyder is the first run.
Restart the kernel and it will work normally.

![Peek 2021-11-05 22-20](https://user-images.githubusercontent.com/22970578/140517226-1bed1163-444e-4fc2-84e5-2ba1fabeae15.gif)

Thank you.


spyder 5.2.0dev f1cfacb

# Mandatory:
atomicwrites >=1.2.0                  :  1.4.0 (OK)
chardet >=2.0.0                       :  4.0.0 (OK)
cloudpickle >=0.5.0                   :  2.0.0 (OK)
cookiecutter >=1.6.0                  :  1.7.2 (OK)
diff_match_patch >=20181111           :  20200713 (OK)
intervaltree >=3.0.2                  :  3.1.0 (OK)
IPython >=7.6.0                       :  7.29.0 (OK)
jedi >=0.17.2;<0.19.0                 :  0.18.0 (OK)
jellyfish >=0.7                       :  None (OK)
jsonschema >=3.2.0                    :  3.2.0 (OK)
keyring >=17.0.0                      :  23.1.0 (OK)
nbconvert >=4.0                       :  6.1.0 (OK)
numpydoc >=0.6.0                      :  1.1.0 (OK)
parso >=0.7.0;<0.9.0                  :  0.8.2 (OK)
pexpect >=4.4.0                       :  4.8.0 (OK)
pickleshare >=0.4                     :  0.7.5 (OK)
psutil >=5.3                          :  5.8.0 (OK)
pygments >=2.0                        :  2.10.0 (OK)
pylint >=2.5.0                        :  2.9.6 (OK)
pyls_spyder >=0.4.0                   :  0.4.0 (OK)
pylsp >=1.2.2;<1.3.0                  :  1.3.0.dev0 (OK)
pylsp_black >=1.0.0                   :  None (OK)
qdarkstyle =3.0.2                     :  3.0.2 (OK)
qstylizer >=0.1.10                    :  0.1.10 (OK)
qtawesome >=1.0.2                     :  1.0.2 (OK)
qtconsole >=5.1.0                     :  5.1.1 (OK)
qtpy >=1.5.0                          :  1.10.0 (OK)
rtree >=0.9.7                         :  0.9.7 (OK)
setuptools >=49.6.0                   :  58.0.4 (OK)
sphinx >=0.6.6                        :  4.2.0 (OK)
spyder_kernels >=2.1.1;<2.2.0         :  2.2.0.dev0 (OK)
textdistance >=4.2.0                  :  4.2.1 (OK)
three_merge >=0.1.1                   :  0.1.1 (OK)
watchdog >=0.10.3                     :  2.1.3 (OK)
xdg >=0.26                            :  0.27 (OK)
zmq >=17                              :  22.2.1 (OK)

# Optional:
cython >=0.21                         :  None (OK)
matplotlib >=2.0.0                    :  3.4.3 (OK)
numpy >=1.7                           :  1.21.2 (OK)
pandas >=1.1.1                        :  None (OK)
scipy >=0.17.0                        :  1.7.1 (OK)
sympy >=0.7.3                         :  1.9 (OK)



Discussion:
Hi @ok97465 thanks for the feedback! I was able to reproduce this! Maybe the issue could be related with the initial console not getting the updated path to the project and then not being able to do the import the first time (I checked without using a project and the preference worked using as code to run `import os.path as osp`)
Note: This happens only if you launch Spyder with an already open project. If you open the project after Spyder started seems like is working

Resolving patch:
diff --git a/spyder/plugins/projects/plugin.py b/spyder/plugins/projects/plugin.py
--- a/spyder/plugins/projects/plugin.py
+++ b/spyder/plugins/projects/plugin.py
@@ -614,7 +614,7 @@ def reopen_last_project(self):
         if (current_project_path and
                 self.is_valid_project(current_project_path)):
             self.open_project(path=current_project_path,
-                              restart_consoles=False,
+                              restart_consoles=True,
                               save_previous_files=False)
             self.load_config()
 


Test patch: