This is my current project folder settings in VSCode:
main_folder
-folder1
__init__.py
main.py
-core
__init__.py
core.py
-checker
__init__.py
checker.py
-venv
The problem arises when I try to import a class from init.py in core_folder by doing this:
from core import MyClass
Funny thing here is that when I right click on VSCode and select Run Python File in Terminal, IT WORKS!
But when I select all the lines in my main.py and run Run Selection/Line in Python Terminal, I get ModuleNotFoundError: No module named core.
My understanding is that since folder1 has __init__.py, and a file from that folder is being executed, all the subfolders with __init__.py will be recognized as modules and I should be able to import from core. This works when running this file in the terminal but why not in the Selection/Line in Python Terminal?
Related
I have the following:
my_project/
hybrik/
__init__.py
models/
__init__.py
builder.py
scripts/
demo.py
And in demo.py:
from hybrik.models import builder
When I tried to run demo.py, an error occured:
ModuleNotFoundError: No module named 'hybrik'
I've already had __init__.py, why can't it find the module?
Python will look for modules in locations on the PYTHONPATH.
Assuming the actual code in those 4 Python files makes sense, you can do the following:
on PowerShell:
$env:pythonpath += ";/path/to/my_project"
on Windows command prompt:
set PYTHONPATH=%PYTHONPATH%;/path/to/my_project
on a Linux shell:
PYTHONPATH=$PYTHONPATH:/path/to/my_project
Alternatively, you can build a package and install it in the environment of your script.
Im creating my own simple package that i will upload on pypi that generates jokes on my native language. While creating the package I'm bugged with this error : ModuleNotFoundError: No module named 'utils'
Here is my folder structure :
Myjokes/
myjokes/
data/
jokes.json
utils/
helpers.py
__init__.py
jokes.py
when i am in the Myjokes/ directory and i run the python -c "from myjokes import jokes" it runs but when i run the functions inside the jokes file it displays a module not found error : from utils.helpers import get_jokes_by_count , get_joke_by_dialect , pinoy_jokes ModuleNotFoundError: No module named 'utils'
if i go inside myjokes/ directory which is the subdirectory of Myjokes i can run the jokes.py script via import jokes and everything is running. i dont know why this is happenin I'm fairly new to python.
Python doesn't know that your utils/ folder is supposed to be treated like a python package unless you include an __init__.py in that folder as well.
Try adding a blank __init__.py file inside utils/ and see if that works for you.
Assume I have the following file structure:
Package/
__init__.py
A.py
B.py
Inside __init__.py I have the following:
__init__.py/
import numpy as np
import pandas as pd
Then I issue the following in the A.py script:
A.py/
from Package import *
However, I receive an error message that no module name Package is defined.
ModuleNotFoundError: No module named Package
I thought from Package import * means running everything in the __init__.py.
I can run the A.py content and use the init import from B.py as I expected.(using from Package import *)
I am using VSCode and Anaconda and my OS is Windows 10.
I can append the project folder every time to my PythonPath using the following commands:
sys.path.append("Path to the Package")
But I do not want to run this piece of code every time.
Can anyone explain what is the problem?
Is this a new problem in Python since I do not recall having such issues in the past?
Because if you run the B.py, the Parent folder of Package folder will be added into the sys.path, be equal to you add sys.path.append("Path to the Package") in the A.py file.
But when you run the A.py, it will add the Package folder instead of the Parent folder of Package folder to the sys.path.
sys.path:
A list of strings that specifies the search path for modules.
Initialized from the environment variable PYTHONPATH, plus an
installation-dependent default.
As initialized upon program startup, the first item of this list,
path[0], is the directory containing the script that was used to
invoke the Python interpreter.
If you are running the python file in debug mode(F5), and the Package folder was the subfolder of your workspace, you can configure the PYTHONPATH in the launch.json file:
"env": {
"PYTHONPATH": "${workspaceFolder}"
},
In your A.py script use just use import file.py and do not use the star. Or, put all your files in a second Package2 directory and use from Package2 import * from your current A.py file.
It is a wired question. I have a project folder and in this folder, I have a project.py and another src folder for my own module, let's say module.py. I also made this src folder as my root source folder.
The project.py can be run successfully(right-click the tab and select run project.py). However, when I copy the whole script into the python console and run it, it says ModuleNotFoundError: No module named 'module'.
Any thoughts?
I have a dir structure .
location of dir is "d:\sources\dir"
/dir
__init__.py
commonlibs/
__init__.py
lib1.py
lib2.py
project1/
__init__.py
prgfile1.py
prgfile2.py
prglib/
__init__.py
prglib1.py
prglib2.py
when i add dir to my PYTHONPATH i can easily import from anything and run the python script.
but when i create my exe from py2exe i get import error of modules present in "commonlibs".
and these modules are imported in prglib1 modules. and some other modules are not imported.
i dont know what i am missing ,but at the end i want to import from any where and my exe should run without error. my exe is an windows service and my project has named pipe implemented.