I've recently switched from Spyder to VS Code to code my Python projects. Spyder is great for me because it uses IPython, i.e., it is based on a REPL (interactive environment), but it still lacks some useful features as code refactoring. In turn, VS Code is superb because it provides a more sophisticated editor, but it is hard to configure properly. In my current project, I have this folder structure:
\SysID
|--\src
|--\app
|--\core
|--\utils
|-- config.py
The root of my project (i.e., my workspace) is the folder \SysID and all my runnable scripts are stored within the \app folder. The custom functions I use are stored in the \core and \utils folders, so that I can't import them directly. In Spyder, I had a script (configure.py) to setup the environment, shown below:
# config.py
# This script configures your environment to run all files in this project
import sys, os
sys.path.append(os.path.join(os.path.dirname(os.getcwd()),'src','core'))
sys.path.append(os.path.join(os.path.dirname(os.getcwd()),'src','utils'))
Every time I opened the project, I had to run that script first. This solution seems a bit awkward, but it works fine - in Spyder. With VS Code though, it is useless because it opens a new Python session every time you run a script. By researching here and there over the internet (including Stackoverflow) I've tried this:
(1) added a cwd key to the launch.json file as below:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${workspaceFolder}"
},
] }
(2) added the following lines to the settings.json file as below:
"terminal.integrated.env.windows": {
"PYTHONPATH": "${env:PYTHONPATH};
${workspaceFolder}/src/core;
${workspaceFolder}/src/utils"}
(3) created a .env file in the root folder of the project with the following text:
PYTHONPATH=${PYTHONPATH};./src/core # Use path separator ';' on Windows.
Nothing worked though. So, folks, what do I have to do to setup the search path of my project as I need? I am using Anaconda to run both Spyder and VS Code.
CodePudding user response:
I solved the issue and will post the answer here as it may help another poor devil trying to solve a similar issue.
The answer turned out to be quite simple. I created a powershell script under the \SysID folder named config.ps1 as:
# config.ps1
$env:PYTHONPATH="$env:PYTHONPATH;$pwd\src\core"
$env:PYTHONPATH="$env:PYTHONPATH;$pwd\src\utils"
echo "Your PYTHONPATH variable has been set."
echo "Its current value is $env:PYTHONPATH"
Everything I need to do is run this script in the same terminal VSCODE uses for the Python sessions. In case one resets or kills the terminal, it has to be called again.
Also, in order to prevent the syntax checking feature from telling me my imports are invalid, I added these lines to my settings.json file:
"python.analysis.extraPaths": [
"./src/core",
"./src/utils",
"./src/app",
"./"
]
All actions I described in my question were completely useless. All that had to be done was to set the PYTHONPATH environment variable by calling the config.ps1 script.