this is my first question and I searched the whole internet for one day and couldn't find a solution.
Hopefully someone can help me here.
I have running Anaconda and Python 3.8.3 64-bit on Windows 10.
I want to import an own program modul from an other file in an other folder .
foldera contains a testa.py file
folderb contains a testb.py file. I want to import foldera.testa
The import does not work. Terminal says ModuleNotFoundError: No module named
VS Code Screenshot
What I tryed:
put the file: __init__.py and .envin every project folder.
Reinstalling both programs and deleted temp files
Looking folder .vscode into settings.json showed that the python.pythonPath is set correctly to python.exe
Typed in python.exe sys.path.append('\\path\\to\\whatever')
and os.environ['PYTHONPATH'] = '\\path\\to\\whatever'
Thank you in advance for the help
Best regards
Sepp
I create a project with the same folder structure as yours, like the following screenshot shows:
In launch.json, you should add
"cwd": "${workspaceFolder}",
when testb.py looks for the module, this setting makes it first to search in the current workspace folder, and the code sys.path.append("./") let it turn to its parent directory, now testb.py is at the same level as foldera, so you can use import foldera.testa without any errors.
Related
My project file structure is like this,
project/src/test/myscript.py
project/src/utils/file_utils.py
When I run myscript.py, which has from utils import file_utils, it gave me error:
ModuleNotFoundError: No module named 'utils'
Previously in Pycharm IDE I did not get this type of error (maybe due to _ init _.py), the subdirs of the same parent dir could be detected. But not sure for VSCode, is there something I need to add for specifying the file structure? And I opened the folder project as my VSCode workspace (not sure if where I open the workspace matters)
I tried adding:
in the /project/.vscode/launch.json
"cwd": "${workspaceFolder}/src"
or in the begining of myscript.py
import sys
import os
src_path = os.path.dirname(os.path.abspath('/project/src/'))
sys.path.insert(0, src_path)
But none of them works. Does anyone have any insights? Thank you very much!
You could consider placing a .env file at the root of your project which adds your source directory to PYTHONPATH. i.e. something like
>>> cat /project/.env
PYTHONPATH=/project/src/
>>>
Your code will look a smidgen nicer without the explicit manipulation of sys.path.
VSCode's usage of .env files is documented here.
Yes, in Pycharm you didn't get this error because it adds __init__.py file automatically when you create a python module. The python identifies the structure of your project through these files, if your folder does not have __init__.py python will understand it as just any folder.
Unlike pycharm, vscode uses the workspace as the root directory to retrieve files. The first method you try is to write it in the launch.json file, which is applicable to debug rather than running it directly. You can use the following code to import:
from src.utils import file_utils
I am working on a python project which consist of several packages and modules. Hence I need to import modules from different packages. However, when importing these modules I am getting import errors. The folder structure is as follows:
Image of folder structure
Within the module Stage0.py I use the relative import: "from ..data.Datapipe import DataFactory" to import the class DataFactory. However when I execute the script I get an error message: "ModuleNotFoundError: No module named 'MT'"
I would appreciate any feedback as I'm becoming desperate
I'm not 100% sure I've understood this correctly, but by the looks of it when you run the Stage0.py it will set the root python path to the app directory, the problem is that python's use of relative imports only allows to find files within that root directory, this site explains it a bit better to be honest
See here
Note that for relative imports, the dots . can go up only up to (but not including) the directory containing the script run from the command line
Anyway, it to fix it you could create a __main__.py file within the mt folder which calls the Stage0.py script, this will set that mt folder as the root of the project giving access to the other directories.
As a simple solution, I recommend that you put Stage0.py in the same location as the requirements.txt and execute it from there
as follows:
-data/
-__init__.py
-Datapipe.py
-graph/
-__init__.py
-s0.py
-stage0.py
stage0.py
from data.Datapipe import DataFactory
I have a sample Python application that I'm playing around with in VS Code. The folder structure that I have is that the application is in a sub-folder. The readme.md and code-workspace files and the like are in the root of the project, and then I have a sub-folder for my application code.
I have a file that represents the entry point of the application, and other files containing supporting logic in the same folder. When I try to import from those files VS Code wants me to put a period before the module (file) name.
from .my_code_file import my_class
But that errors when I run the application. It runs just fine if I remove the leading period.
from my_code_file import my_class
Why does VS Code Intelisense think I need a period in front of the module name?
This is a representation of the file and folder structure:
|-AppCode
| |-my_code_file.py
| |-application_entry_point.py
|
|-readme.md
|-MyProject.code-workspace
The reason is that when importing other files, the standard import statement should be: "from AppCode.my_code_file import my_class", and VScode starts from the parent folder of the currently opened file by default. Obviously, VSCode cannot find the folder "AppCode" from the folder "AppCode", we can use the following two methods to help VSCode find "my_code_file":
Please add the following statement, which adds the path of the file to be imported to the system path, and VSCode can find her in the system path:
import os,sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
If Pylint reports an error "Unable to import'AppCode.my_code_file", but the code can run, we can use
"python.linting.pylintArgs": [
"--disabled=E0602"
]
to turn off this warning.
2.We can also use "from my_code_file import my_class", the code can be executed to get the result.
If Python reports an error "unresolved import'my_code_file", but the code can run, we can use
"python.analysis.disabled": [
"unresolved-import",
],
to close this warning.
Reference: readable-pylint-messages.
I was running into issues just like this recently and after some googling I found several ppl recommending I download microsoft's new Python extension called Pylance.
I've been using it for about a week and it solved all of my issues I was having that involved my application being confused w/ file structure. I'd recommend giving it a try!
https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance
Cheers,
-Andrew
1. The environment
I am working in Windows 10 with Anaconda/Spyder tools.
I have a python project organized as follow.
folder_project
test_all.py
folder_utils
__init__.py
function1.py
folder_tests
__init__.py
test_function1.py
The init.py files are empty files.
Basic idea for implementing code coverage of my code (maybe methodology can be improved) is that I will have:
one python file to test in different manners one function
test_function1.py will contain one function per test:
def test_001():
...
test_all.py file will call all functions for all test_functionXXX.py files
In Spyder, I have taken care to add in PYTHONPATH the folder_project.
2. The problem
I encounter error in importing the folder_tests package...
I have in test_all.py
import folder_tests.test_function1
def main():
# Testing function1
test_function1.test001()
And I get following error:
ModuleNotFoundError: No module named 'folder_tests.test_function1'
If I only do
import folder_tests
I have no error message, but then I can do nothing with that...
If I change the import statement in
from folder_tests import test_function1
I get following error:
ImportError: cannot import name 'test_function1' from 'folder_tests'
Please, any idea how I could correct that?
I thank you in advance for your help!
Bests,
Pierrot
The origin of the error doesn't seem related to python, but spyder.
I stopped and re-launched spyder, it didn't work, I simplified name of files, and it worked, and I then re-used the previous file names, and it still works...
Kind of a headache here...
Sorry for the bother.
Bests,
Pierrot
Pierrot, regarding your answer I agree, it is a total headache but I got to the bottom of the issue for 2 scenarios:
If the folders which you referenced are not housed in a folder that is recognized by Spyder as a project folder then this issue occurs. In your case this would be folder_project. This can be tested by going to Projects->Open Project if the folder you select is not recognized as a project folder it will tell you, and not open it. Can be fixed by Project->New Project->Existing Directory
Folder name the same as file name (Spyder specific, not an issue using other IDEs such as pycharm)
I had the folder name the same as the file name e.g. folder called 'email' and file inside called 'email.py'. When I tried to import, it raised 'ModuleNotFoundError: No module named 'email'.
Here's the version that failed with matching file and folder name:
import email.email as e
Here's the version that worked with differentiated file (added and 's') and folder name:
import emails.email as e
I have a piece of code located in a file (utils.py) in a folder different from the one my current script is located in. I tried:
from "/Z/scripts/utils.py" import *
but it gives a syntax error. Is there a way to "include" my own code located elsewhere other than the current folder?
You need to add that directory to your python path
import sys
sys.path.append("/Z/")
from scripts.utils import *
Make sure the scripts directory contains an __init__.py file
You can import code that is in a directory if it is added to PYTHONPATH. See here for more details.