How to fix "no module named 'app_one'" - python

I have a Python package with the following structure.
>python_package # package root directory
>app_one # subpackage directory
>__init__.py
>views.py
>app_two # another subpackage directory
>__init__.py
>views.py
Code for app_one/views.py:
def show():
print('do something')
Codes for app_two/views.py:
from app_one.views import show
show()
The problem is, whenever I try to run views.py of app_two from the terminal, I get an error
No module named 'app_one'
But when I open the package python_package in the PyCharm IDE, I'm getting no issue, everything works perfectly.

This error occurs because, the path to the file app_one is not in the current path, and you have to add it to the path using sys.path.append Try :
import sys
sys.path.append('./app_one')
from views import show
show()

I have created the same directory structure as you had and gave it a try and it is working.
I think what you were missing is that adding this line before you import in app_two/views.py:
sys.path.insert(0, os.path.abspath(__file__ + "../../../"))
Please have a look at the attached image which has detailed information

Related

main files and module import errors

Hi, I've been stuck on this problem a while, and all other questions around this issue on this site haven't helped fix the issue.
In the main.py file, I'm trying to import the datastorer function which sits in the StoreData.py file.
I've created empty _init_.py files in the project root, DataStore
folder and also Calculations folder.
I'm importing using :
from project.DataStore.StoreData import datastorer
But always get the error
ModuleNotFoundError: No module named 'project.DataStore'.
I've tried adding
myDir = os.getcwd()
sys.path.append(myDir)
to the top of the file which wasn't successful.
I'm wondering if it's something to do with the name of the file main. Although I need to keep this as the file name. If anyone has any ideas this would be great thank you!
If you add your current folder to python path, then all you imports should not refer to project so try
from DataStore.StoreData import datastorer
At the top of main.py you can add
import sys
from os.path import dirname
sys.path.append(dirname(dirname(dirname(__file__))))

get error when trying to access a file present in other directory

Please look at the image below to see the project dir structure. I have a root dir called UI which contains 2 sub dir called test1 and test2. I have a file called C.py in test2 dir that needs a function present in A.py inside teste1.
I get error when I try to access the package as from UI.test1.A import Atest. I have added init.py (Empty files) as you can see but I still get module not found error
A.py
class Atest:
def printA(self):
print("A invoked")
I get the same error when running from root
You're trying to run it from UI/test2. You should be higher in the arborescence or use complete path
Try using this snippet.
import sys
sys.path.insert(1, '../')
You are running your script in subdirectory (~/UI/test2) and import is trying find UI module under that directory, i.e. the folder where the script you pass to python is located. The __main__ entrypoint should always be at the root of your project so you don't mess other imports with having to append to path.
You can see the places where python can find packages with:
import sys
print(sys.path)

I am getting an import error. How to fix this?

I have the following directory structure:
C:/Automation/Windows/bin/powerconfig/powerconfig.py
C:/Automation/Windows/modules/Pylog.py
I am in the directory of:
C:/Automation/Windows/
When I try to run powerconfig.py file from a windows directory like
C:\Automation\Windows> pyhton \bin\powerconfig\powerconfig.py
I instantly get the error of no module named 'modules.Pylog'
Powerconfig.py contain import statement like
from modules.Pylog import Pylog
All directory contains __init__.py file to consider package
Even I tried to fix error by adding path C:/Automation/Windows/ to sys.path but still I am getting same error.
I don't know how to fix this error.
Try add the following code in Powerconfig.py
import sys
sys.path.append("C:/Automation/Windows/")
from modules.Pylog import Pylog

Import classes/functions from Python files saved in same folder showing "No Module Named" error

As per screen print, import shows error in Python 3.7 version, earlier it was working fine in version Python 2.7 and I am using IntelliJ Idea.
If you see, EOC related .py files are in the same folder and have classes which are being called in Main_EOC.py by passing objects which are inter-related. It's amazing to see the red line while importing files from same folder.
Please help me why it's showing such error
"This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.`"
Also, if you see the line which have full path, is not showing error
from EOC_Module.eoc.script.config import Config
Please help me if there is a way to add this full path on top of the code or other option.
The behavior of import path search changed between python2 and python3. The import path always includes the directory from which the main module was loaded, but it no longer includes directories from which modules were imported.
You need to change your import statement syntax as follows, if you want to import a module that lives in the same directory as the module in which you do the import:
# old way, import works if the named module is in this module's directory
import x
# new (Python3) way:
from . import x
For the second part: adding a path so all code can import from a certain directory: if that directory is (and will always be) relative to your main: you can add a few lines in the main module to make it available. Something like this:
import sys # if you haven't imported it already
import os.path
home = os.path.dirname(sys.argv[0])
sys.path.append( os.path.join(home, "EOC_Module/eoc/script") )
# now, you can import straight from the script directory
import EOC_Intraction
When using pycharm the root directory for your python executable is the same as the root directory of your project, this means that python will start looking for files in the root directory with this files:
.idea/
EOC_module/
logs/
reports/
sql/
This is the reason of why: from EOC_Module.eoc.script.config import Config works.
If you execute your code from the terminal with: python3 Main_EOC.py (not pycharm) the root directory for your python will be the same as the one containing the file, all the other imports will work but from EOC_Module.eoc.script.config import Config not.
So you need to make your imports from project directory if you are using pycharm.

weird python3 import issue, No module named <module>

I write some python files like this:
main.py
view/ __init__.py #empity file
MainWindow.py
ListEditor.py
And in each file I wrote those imports:
<main.py>
from view.MainWindow import MainWindow
...
-
<MainWindow.py>
from view.ListEditor import ListEditor
and ListEditor.py don't import any files.
Each MainWindow.py or ListEditor.py defines a class that named same as the file name.
when I run the program from main.py, it works. But when I run from MainWindow.py I got ImportError: No module named 'view'
If I write
from ListEditor import ListEditor
in MainWindow.py, python MainWindow.py will be OK. but python main.py will get error:
ImportError: No module named 'ListEditor'
So, is there a way to make both python main.py and python MainWindow.py get right at the same time?
I'm using python3.4
P.S.
I think I have figured out the problem here. The import command searches a module in sys.path. The sys.path is a group of predefined paths plus the running script path. When I run the code from MainWindow.py, the code import ListEditor just works, but when I run from main.py, the current path is set to the parent path. So I need import view.ListEditor.
Well, there are couple ways to deal with it. #Vincent Beltman's answer is one of it. Or just put these code in the __init__.py file:
import os, sys
path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(path)
Finally, I'm new to python. And I think the import command is quite strange. I thought it should search the files relative to the path of the source file that containing the command, not just relative to the starter file. A starter file may varying and cause troubles like this one.
Try this:
try:
from view.ListEditor import ListEditor # If this one fails
except:
try:
from ListEditor import ListEditor # It will try this one

Categories

Resources