VSCode run produces ModuleNotFoundError when I try to import my module - python

I have a project folder that has a module called 'src' containing my code. Within 'src', there is another module called 'crawlers' which has python code in it. In one of the files stored in the 'crawlers' folder, I call an import from the 'src' folder to get variables from its init.py file.
The structure is like this
└───src (this is a module)
├───crawlers (this is a module)
|----- code.py
├───datafiles
├───models
└───utils
in the code.py file I have
import json
from src import variable1, variable 2
When the code runs in vscode, I get this
Traceback (most recent call last):
File "d:/Code files/tech projects/tree_courses_scraper/src/crawlers/course_grab.py", line 2, in <module>
from src import umanitoba_calendar, umanitoba_codes
ModuleNotFoundError: No module named 'src'
I have been using pycharm community for a while and I never got this problem.

Related

Do I have a problem with __init__.py files?

__init__.py files are meant to initialize packages and/or to set what directory is a package, as far as I know.
So my question is, even though these are implicitly run when a package/module is imported, is it normal to receive errors when you run that __init__.py file directly? and why?
I was testing a project I'm currently working on, which is working great, no errors when I import from root files, but what got my attention is that when I execute every module just to check for errors, all the __init__.py files are the ones that throws errors.
For the sake of education,
I followed these 'tutorials' and ran every __init__.py files of every package (directly, not importing a module/package) just to identify if I'm doing something wrong and received errors on those too.
Please, take a look at these examples:
Article: Understanding Python imports, __init__.py and pythonpath — once and for all
Traceback (most recent call last):
File "c:\Users\username\Desktop\python_packages_tutorial\utils\__init__.py", line 1, in <module>
from utils.lower import to_lower
ModuleNotFoundError: No module named 'utils'
I even added the directory to PYTHONPATH env variable, and sys.path even recognizes the directory where it is located. As I said before, it's just happening when I directly execute the init.py file, the rest is working well.
Article: Introduction to Create Own Python Packages
Traceback (most recent call last):
File "c:\Users\username\Desktop\medium_python_packages\my_package\__init__.py", line 1, in <module>
from .my_module1 import *
^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: attempted relative import with no known parent package
My project __init__.py file:
sys.path
[
'c:\\Users\\username\\Desktop\\code\\projects\\flask_app\\register',
'C:\\Users\\username\\Desktop\\code\\projects\\flask_app\\register', <-- PYTHONPATH env
'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python311\\DLLs',
'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python311\\Lib',
'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python311',
'c:\\Users\\username\\Desktop\\code\\projects\\flask_app\\flask',
'c:\\Users\\username\\Desktop\\code\\projects\\flask_app\\flask\\Lib\\site-packages'
]
Traceback
Traceback (most recent call last):
File "c:\Users\username\Desktop\code\projects\flask_app\register\__init__.py", line 10, in <module>
from register.routes import *
ModuleNotFoundError: No module named 'register'
But they are working as expected.

Python3.6 ImportError when running script outside the directory. Runs correctly within same directory

I have the following set up.
~/python/pyct/lib/
├── printer.py
└── utils.py
~/apps/proj/
└── main.py
~/python/pyct/lib/utils.py
def printFunc(content):
print(content)
~/python/pyct/lib/printer.py
import utils # this breaks sometimes
# import pyct.lib.utils as utils # this works always
def printer(content):
utils.printFunc(content)
~/apps/proj/main.py
from pyct.lib.printer import printer
printer("hi")
Value of PYTHONPATH=$HOME/python
When I import printer.py in a file inside ~/python/pyct/lib/, everything runs as expected.
When I run main.py from ~/apps/proj/, I get the following error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
from pyct.lib.printer import printer
File "~/python/pyct/lib/printer.py", line 1, in <module>
import utils
ModuleNotFoundError: No module named 'utils'
I have tried using relative imports but that doesn't work. I have gone through this resource but could get the scenario working.
I am aware that the problem can be solved using absolute path in imports but I want to make a dir with multiple python files which will act as a library. Then use the core component else where outside the directory.
How can I achive the above scenario working.
Any resources to understand python imports will be very helpful.
The problem here is that ~/python/pyct/lib/printer.py looks for module named utils in its working directory - not in directory where it is placed itself.
You can use relative import in ~/python/pyct/lib/printer.py:
from . import utils
python will then look for module relative to the path of the importing one instead of working directory.

Unable to import module from Python

I am trying to import
from app.idol.IP import CONTENT_IP_ADDRESS, CONTENT_PORT
I get the following error
Traceback (most recent call last):
File "testcontent.py", line 1, in <module>
from contentactions import get_dbs
File "C:\Code\Python\xxxxx\app\idol\content\contentactions.py", line 1, in <module>
from app.idol.IP import CONTENT_IP_ADDRESS, CONTENT_PORT
ModuleNotFoundError: No module named 'app'
See directory structure.
All imports are relative to the directory where you run Python. From your screenshot it is clear that you are running testcontent.py from app\idol\content rather than main.py at the source root, so it fails to find the subdirectory app within app\idol\content.
You can instead run Python from C:\Code\Python\K2Associates like this:
python app\idol\content\testcontent.py
__init__.py is imported using a directory. if you want to import it as app you should put __init__.py file in directory named app
another option is just to rename __init__.py to app.py

ImportError: No module named utils.read

I have a main.py file in the folder project and read.py in the folder ./project/utils. In the main.py, I called
import sys, os
sys.path.append('./utils/')
from utils.read import function1
However, when I use the python main.py command, I got the error
ImportError: No module named utils.read. What should I change? Thanks all
i think you need to add __init__.py
in your directory..
make sure you have __init__.py in utils folder.. then only python will understand it is package folder contains py
__init__.py specifies that the folder is actually a package. a package in python is a directory containing .py files (modules). In every folder which is a package (that is, a folder containing multiple .py files) you should define __init__.py.
It can be empty, or you can put some statements to execute when the package is imported (or modules from the package).
For exmaple, let's take this directory tree:
/dev/package/greeter.py
and current working directory is /dev.
>>> from package import greeter
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
from package import greeter
ImportError: No module named package
import pakcage results in the same error. When adding __init__.py into the package folder, it works. My init is simple as
print 'init executed'
>>> from package import greeter
init executed
>>>
One common functionality to put in __init__.py is the __all__ variable. You can read more about it here Can someone explain __all__ in Python?

ImportError: No module named test when ___init__.py is included

I created a python project in this format:
I tried to run my test_jabba.py by cding into the tests directory and running the program and I received this error:
Traceback (most recent call last):
File "./test_jabba.py", line 12, in <module>
from tests import testbench
ImportError: No module named tests
I read around and I realized I needed __init__.py to tell python where other packages are located.
Top portion of test_jabba.py
from tests import testbench
from utils import gopher, jsonstream
I did not add __init__.py into my logs and resources directories as they do no contain any python.
My best guess would be that poc is not in your PYTHONPATH. You can either set/extend the environment variable to contain poc, or
you can manipulate the path in your script using os.path. Your imports, in this case, will have to change accordingly:
from poc.tests import testbench
from poc.utils import gopher, jsonstream
Alternatively, you can use a relative import, to import tests and utils:
from ..tests import testbench
from ..utils import gopher, jsonstream

Categories