All was good until I start to change the "launch.json" file for some other issues. However, I made it the same as before:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{"name":"Python: Current File","type":"python","request":"launch","program":"${file}","console":"integratedTerminal"},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
after that when I run my code, the first several lines to import module, e.g. import pandas as pd,
then it shows me ModuleNotFoundError: No module named 'pandas'.
but if I try to import in Jupyter, no issue at all.
Not sure if I should uninstall VScode or something. Thanks for any advice!
reply to myself, I haven't figured out yet, but I think it's related to the python path.
Related
I have the following folder structure. Project1 and Project2 are part of a multi-root workspace.
I will be developing on windows, but running on linux. so i would like like to keep different environment files (.env and .env_linux), and load them based on the OS running under. The .env file looks like this:
PYTHONPATH=./src:./src/utils:./src/app:./src/services
My launch.json file looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Run App",
"type": "python",
"request": "launch",
"program": "${workspaceFolder:project1}/src/app/app.py",
"console": "integratedTerminal",
"justMyCode": true,
"windows": {
"envFile": "${workspaceFolder:project1}/.env"
},
"linux": {
"envFile": "${workspaceFolder:project1}/.env_linux"
}
}
]
}
The code in app.py looks like below - just one line trying to import the utils module:
from utils import utils
When the code runs, at the above line I get the error "No module named 'utils'"
So i next added the following to my settings.json:
"terminal.integrated.env.windows": {
"python.envFile": "${workspaceFolder:project1}/.env",
},
"terminal.integrated.env.linux": {
"python.envFile": "${workspaceFolder:project1}/.env_linux",
},
This did not solve the problem. I figured that this env file approach just isnt going to work and then added the PYTHONPATH to the settings.json as seen below, but i still get the same error:
"terminal.integrated.env.windows": {
"python.envFile": "${workspaceFolder:project1}/.env",
"python.pythonPath":"${workspaceFolder:project1}/src:${workspaceFolder:project1}/src/app:${workspaceFolder:project1}/utils",
"PYTHONPATH":"${workspaceFolder:project1}/src:${workspaceFolder:project1}/src/app:${workspaceFolder:project1}/utils",
},
Still the same error. I also tried changing the .env file to reference the absolute path to no avail. What am i doing wrong???
It's interesting to note that pylance is able to find the packages/modules when editing. just at run time i get that error.
For the initial problem,
We use ; to split environment variables instead of :.
I installed sympy using the commands
source website-env/bin/activate
(website-env)pip install sympy
My Python script is
import sympy as sp
u1,u2,d1,d2=sp.symbols('u1,u2,d1,d2')
psip,psim,phip,phim=sp.symbols('psip,psim,phip,phim')
psi=u1*(phip-phim)*u2-u1*(psip-psim)*d2-d1*(psip+psim)*u2+d1*(phip+phim)*d2
sp.factor(psi,phip,phim,psip,psim)
It runs fine from the command line but when I select this Python interpreter and try to run it within VSCode, it give "module not found - sympy"
My launch.json is
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
What could be the problem?
It seems there was something wrong with my virtual environment. I used pip freeze to get a list of modules, then deleted the environment and recreated it. That fixed it.
I recently started exploring VS Code for developing Python code and I’m running into an issue when I try to import a module from a subfolder. The exact same code runs perfectly when I execute it in a Jupyter notebook (the subfolders contain the __init__.py files etc.) I believe I followed the instructions for setting up the VS Python extension correctly. Everything else except this one import command works well, but I haven’t been able to figure what exactly is going wrong.
The structure of the project is as follows: The root folder, which is set as the cwd contains two subfolders (src and bld). src contains the py-file that imports a module that is saved in foo.pyin the bld-folder using from bld.foo import foo_function
When running the file, I get the following error: ModuleNotFoundError: No module named ‘bld'. I have several Anaconda Python environments installed and get the same problem with each of them. When copying foo.py to the src directory and using from foo import foo_function everything works.
My launch.json file is as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {"PYTHONPATH": "${workspaceFolder}:${workspaceFolder}/bld"},
"console": "integratedTerminal"
}
]
}
Any ideas or help would be greatly appreciated!
Stefan‘s method worked for me.
Taking as example filesystem:
workspaceFolder/folder/subfolder1/subfolder2/bar.py
I wasn't able to import subfolders like:
from folder.subfolder1.subfolder2 import bar
It said: ModuleNotFoundError: No module named 'folder'
I added to .vscode/settings.json the following:
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}"
}
I also added at the beginning of my code:
import sys
#[... more imports ...]
sys.path.append(workspaceFolder)
# and then, the subfolder import:
from folder.subfolder1.subfolder2 import bar
Now, it works.
Note: all my folders and subfolders have an empty file named __init__.py. I still had to do the steps described above.
VSCode version: 1.52.0 (from 10-dec-2020)
I think I finally figured out the answer myself: The integrated terminal does not scan the PYTHONPATH from the .env-file. When running the file in an integrated window, the PYTHONPATH is correctly taken from .env, however. So in order to run my script in the terminal I had to add the terminal.integrated.env.* line in my settings.json as follows:
{
"python.pythonPath": "/anaconda3/envs/py36/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.flake8Enabled": false,
"python.envFile": "${workspaceFolder}/.env",
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}"
}
}
I have the following directory structure on Windows:
\Code
\ Projects
\ProjectA
\dev.env
\FileA.py
\Shared
\ModA
\__init__.py
\tools.py
Within my ProjectA, Im trying to import a function from tools.py for use in FileA.py using:
from ModA.tools import function_x
I have created an dev.env file in the ProjectA folder that contains a single line:
PYTHONPATH=C:/Code/Shared/ModA/
and I have set the following in VSCode Settings | Workspace Settings
python:Env
${workspaceFolder}/dev.env
But every time I try the import I get a ModuleNotFoundError
What am I doing wrong?
Create the launch.json file following this steps and add this line:
"envFile": "${workspaceFolder}/dev.env"
Your .vscode/launch.json should look like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"envFile": "${workspaceFolder}/dev.env"
}
]
}
Make sure to change the python.envFile option in the Workspace settings (.vscode/settings.json) to ${workspaceFolder}/dev.env
Acording to the documentation:
You can then set the python.envFile setting to
${workspaceFolder}/prod.env, then set the envFile property in the
debug configuration to ${workspaceFolder}/dev.env.
And change the import to:
from tools import function_x
Suppose I have got python module foo.py and file myfile.txt that reside in the same directory. foo.py contains the following code to read myfile.txt:
from os import path
myfile_path = path.join(path.dirname(__file__), 'myfile.txt')
myfile = open(myfile_path)
I found myself writing path.join(path.dirname(__file__), '...') over and over again in different modules. Is there a shorter and simpler way to read a file from the same directory as the python module ?
I don't know if this applies to your exact situation, but here is what I have found:
There should be a JSON file that defines how the debugger should work. For me, I use VS Code, and I use the Microsoft Python debugger. It uses a file called launch.json, and contains an array called "configurations":
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
]
It is missing a key called "cwd", short for "current working directory" (you can read more about it here). If you add it with the value of blank quotes "", then when Python searches for a file without a specified path, it will search within the same folder. Showing the last few lines of what that looks like:
"program": "${file}",
"console": "internalConsole",
"cwd": ""
]
For more general advice, your debugger, or whatever else is running your Python file, needs to have its working directory correctly defined.
I realize the post is nineteen months old at some point, but I hope this helps someone!
You can use data = open('myfile.txt', 'r').read(), without using path.