You are most probably trying to execute it as a shell script. Assuming you are using a linux/unix-like operating system, try adding the following line at the start of the script:
`#!/usr/bin/env python`
Also make the file executable:
`sudo chmod +x sql.py`
Answer from Alexander Ejbekov on Stack Overflowpython - Why can't the import command be found? - Stack Overflow
[BUG] ImportError: ImportError: __import__ not found Python 3.7
I'm lost "no module named...", how importing works?
Python Module Importing Problems
file structure:
Fast-API-Two/
βββ app
β βββ calculations.py
β βββ database.py
β βββ __init__.py
β βββ main.py
β βββ models.py
β βββ routers
β β βββ comment.py
β β βββ __init__.py
β β βββ login.py
β β βββ post.py
β β βββ user.py
β β βββ vote.py
β βββ schemas.py
β βββ utilities
β βββ b_pass.py
β βββ __init__.py
β βββ oauth2.py
βββ requirements.txt
βββ tests
βββ __init__.py
βββ test_calculations.pycontent of app/calculations.py:
def add(num1: int, num2: int=2) -> int:
return num1 + num2content of tests/test_calculations.py:
from app.calculations import add
def test_add():
print("testing function")
assert add(3, 4) == 7
test_add()
When I try ton run python3 tests/test_calculations.py I get ModuleNotFoundError: No module named 'app' this error, however pytest runs without any problem.
From what I understand import in tests/test_calculations.py should look like this:
from ..app.calculations import add # instead of: # from app.calculations import add
I'm leaving tests/ directory and now I should have access to app package, but this time upon running python3 tests/test_calculations.py I get this error ImportError: attempted relative import with no known parent package and pytest throws this one: ValueError: attempted relative import beyond top-level package
Lastly VSCode has no problem with recognizing/reading both of these methods and doesn't show anything like for example Import ".app.calculations" could not be resolved Pylance. Edit: Just to be precise nothing runs when I hit play button in VSCode I'm talking only about Pylance
[Ubuntu 20.04]
Hi all,
I was wondering if anyone had encountered this type of issue before. I am trying to import a module from a package I created. The package is in the same directory as my main.py file, but python is unable to use files in that directory I am using to initialize it's objects.
My structure is:
top level/
main.py
pckgfolder/
__init__.py
mymodule.py
init.txtie, in main.py i have:
from pckgfolder import mymodule myobject = mymodule.myClass()
and then within mymodule.py I have
class myClass:
def __init__(self):
with open('init.txt', 'r'):
#load default dataWhen I try to do this I get:
FileNotFoundError: [Errno 2] No such file or directory: 'init.txt'
Since python cannot see the file I guessed that we need to add the directory with the module to the path, however I checked this is and it is in sys.path at runtime, so the file should be visible.
I added some print statements to __init__.py and found that when it is run the working directory is the top level, so I assume that is why the file is not visible, but do not know what to do about this (I do not wish to try things like changing the working directory during the run). When I was testing the module itself I did so from inside the directory and it worked fine, so I am quite lost here.
Hey Guys,
I get a weird ModuleNotFoundError in one of my projects and i do not really understand why. So here is my Folder Structure:
Project\
| main.py
| packageabc\
| | __init__.py
| | abc.py
| | packagexyz\
| | | xyz.py
In every .py file is an abc/xyz or main class. Now the interesting part. Inside the abc.py I do an import like this:
from packagexyz.xyz import xyz
This works perfectly fine!
Inside the main.py I do an import like this:
from packageabc.abcimport abc
Now I get an ModuleNotFoundError. Error Code looks like this:
from packagexyz.xyz import xyz ModuleNotFoundError: no module named packagexyz.
So the import in the abc.py File that works perfectly when tested inside the file does not work if the abc.py file is imported. So why is that?