Directory structure:
.
├── classification
│ ├── lstm_repres.py
│ ├── lstm_test.py
│ ├── lstm_train.py
│ ├── lstm_utils.py
│ ├── lstm_utils.pyc
│ └── svm_run.py
├── utils
│ ├── data_splits.py
│ ├── evaluation.py
│ ├── load_data.py
│ ├── NLTKPreprocessor.py
│ ├── resources.py
│ ├── TfIdf.py
│ └── vector_utils.py
at lstm_train.py:
from classification.lstm_utils import *
when i try to run with both python2 lstm_train.py or python3 lstm_train.py, i get:
Traceback (most recent call last):
File "classification/lstm_train.py", line 7, in <module>
from classification.lstm_utils import *
ModuleNotFoundError: No module named 'classification'
The same happens at the line
from utils.data_splits import get_train, get_val
What is the issue here? Why does the format from <dir>.<script> import * doesn't seem to be working here?
EDIT
As suggested by #deathangel908, I've added __init__.py empty files to both directories, but the error persists.
In order to mark directory as a package you want to import, it needs to have least empty __init__.py within that directory. So add __init__.py files to classification and utils directories.
In order to import absolute path package you need to run python interpreter from the root package as a module: python3 -m "classification.lstm_train"
Related
Currently I'm working on a python application to import another three self-defined packages in the same directory. Everything went smoothly on my Linux machine. However, while trying to deploy on another windows11 machine, the relative import mechanism seems to become malfunctional. The second package cannot be import successfully. The detail information list below.
directory structure
.
├── COGNet_lite
│ ├── __init__.py
│ └── src
│ ├ ...
│ └── COGNet.py
├── MICRON_lite
│ ├── __init__.py
│ └── src
│ ├── ...
│ └── MICRON.py
├── PCCNet
│ ├── __init__.py
│ └── src
│ ├── ...
│ └── SafeDrug.py
└── tmp.py
./tmp.py
from PCCNet import (
seed,
sample_desc_d,
sample,
data_voc_convert,
icd9_desc,
atc3ToDrugs,
SafeDrug_main,
SafeDrug_parser
)
from COGNet_lite import COGNet_main
from MICRON_lite import MICRON_main
./PCCNet/__init__.py
from .src.select_pat import sample_desc_d, sample, data_voc_convert, seed
from .src.util import icd9_desc, atc3ToDrugs
from .src.SafeDrug import main as SafeDrug_main, parser as SafeDrug_parser
./COGNet_lite/__init__.py
from .src.COGNet import main as COGNet_main
error message on windows11 with python3.7.13
(python37_env) W:\data\fix_tmp\medrec>python tmp.py
Traceback (most recent call last):
File "tmp.py", line 11, in <module>
from COGNet_lite import COGNet_main
File "W:\data\fix_tmp\medrec\COGNet_lite\__init__.py", line 1, in <module>
from .src.COGNet import main as COGNet_main
ModuleNotFoundError: No module named 'COGNet_lite.src.COGNet'
(python37_env) W:\data\fix_tmp\medrec>python --version
Python 3.7.13
I made a very simple python package by following the packaging guide:
tester
├── A.py
├── B.py
└── __init__.py
The contents of A.py are simply:
import B
__init__.py and B.py are empty files.
I uploaded the package to testpypi and installed it in a virtual environment. I started first by checking if it works by making a simple test.py file:
from tester import A
Then I try to run that file:
(scratch-venv) [ccn#sy337b4 scratch-box]$ python test.py
Traceback (most recent call last):
File "/home/ccn/scratch-box/test.py", line 1, in <module>
from tester import A
File "/home/ccn/scratch-box/scratch-venv/lib/python3.9/site-packages/tester/A.py", line 1, in <module>
import B
ModuleNotFoundError: No module named 'B'
I'm confused because when I was developing the package running python A.py never caused an error, but now when I'm importing A it doesn't work any more.
(within the venv, aka: client using package) I was able to fix this by changing the contents of A.py to :
from tester import B
But that's not a solution because when I go back to developing the package I get this error when running python A.py
Traceback (most recent call last):
File "/home/ccn/tester_package/src/tester/A.py", line 1, in <module>
from tester import B
ModuleNotFoundError: No module named 'tester'
Finally for full context I will post the structure of the package. I am developing in src/tester
.
├── build
│ ├── bdist.linux-x86_64
│ └── lib
│ └── tester
│ ├── A.py
│ ├── B.py
│ └── __init__.py
├── dist
│ ├── example_pkg_2_cuppajoeman-0.0.1-py3-none-any.whl
│ └── example-pkg-2-cuppajoeman-0.0.1.tar.gz
├── LICENSE
├── pyproject.toml
├── README.md
├── setup.cfg
└── src
├── example_pkg_2_cuppajoeman.egg-info
│ ├── dependency_links.txt
│ ├── PKG-INFO
│ ├── SOURCES.txt
│ └── top_level.txt
└── tester
├── A.py
├── B.py
├── __init__.py
└── __pycache__
└── B.cpython-39.pyc
9 directories, 17 files
If I remember correctly, in tester/a.py you should have either one of those:
from . import B
from tester import B
import tester.B
I have a project with directory structure like below
.
├── Pipfile
├── Pipfile.lock
├── module
│ ├── __init__.py
│ ├── helpers
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ └── __init__.cpython-36.pyc
│ │ ├── dynamo.py
│ │ └── logger.py
│ └── test.py
Relavant code
logger.py
import click
import sys
from tabulate import tabulate
def formatter(string, *rest):
return string.format(*rest)
def info(*rest):
"""Write text in blue color
"""
click.echo(click.style('☵ ' + formatter(*rest), fg='blue'))
test.py
import helpers
helpers.logger.info('Trying')
When I try to run using the command
python3 module/test.py
I get this error
Traceback (most recent call last):
File "module/test.py", line 4, in <module>
helpers.logger.info('Trying')
AttributeError: module 'helpers' has no attribute 'logger'
I have tried restructuring the code. Putting the helpers directory outside, in level with module directory. But still it didn't work, which it should not have, from what I have read. I tried researching a bit about __init__.py and python module system. The more I read, the more confusing it gets. But from whatever I learned, I created another sample project. With the following structure,
.
└── test
├── __init__.py
├── helpers
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ └── quote.cpython-36.pyc
│ └── quote.py
├── index.py
├── logger
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ └── info.cpython-36.pyc
│ └── info.py
Code is same as first project.
Here when I do,
python3 test/index.py
It works as expected. The only difference between the two projects:
In the first project, I used pipenv to install deps and create virtual environment.
Using your initial layout (with loggers as a submodule of the helpers package), you'd need to explicitely import loggers in helpers/__init__.py to expose it as an attribute of the helpers package:
# helpers/__init__.py
from . import logger
logger is module and not attribute and helpers.logger evalutes logger as attribute. Actually you should do as follow:
from helpers import logger
print(logger.info('Trying'))
I'm trying to import the module my_app/common/model/Model.py into the file /my_app/common/database/db_update.py. My PYTHONPATH points to app/common/. However, when I try to do so, I get the error message:
File "/home/me/my_app/common/database/db_update.py", line 26, in <module>
import model.Model
ImportError: No module named Model
A simplified representation of my file structure is as follows:
common/
├── customlogger
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── logger.py
│ └── logger.pyc
├── database
│ ├── DBSetup.py
│ ├── DBSetup.pyc
│ ├── db_update.py
│ ├── db_update.pyc
│ ├── __init__.py
│ └── __init__.pyc
├── __init__.py
├── __init__.pyc
└── model
├── __init__.py
├── __init__.pyc
├── Model.py
└── Model.pyc
The head of the db_update.py file contains the following imports (amongst others) in this order:
import customlogger.logger
import database.DBSetup
import model.Model
Some information that may be useful:
I have tried importing other modules from the model/ directory. These are also not recognised and result in the same error (with the respective module's name instead).
I am running a bash script to do this, from a directory adjacent to common/ - call it my_app/src/.
All my __init__.py files are empty.
I'm running Ubuntu 14.04.
It seems to me that python recognises the model/ directory (else, the error message would read ImportError: No module named model.Model), but seems to think that it doesn't contains any modules despite the presence of the __init__.py file. Is this the case? And if so, why does this happen exclusively in the model/ directory and not the database/ nor the customlogger/ directories? Finally, how can I fix this?
Many thanks in advance.
I'm trying to test out an example from a book and I'm getting an ImportError.
The example starts out like:
from tkinter import *
from PP4E.Gui.Tools.widgets import frame, button, entry
And if I put an import sys; print(sys.path) at the beginning of the code, the output is
['/Users/aaa/Documents/workspace/programming-python/PP4E/Lang/Calculator',
'/usr/local/lib/python3.4/site-packages/setuptools-12.2-py3.4.egg',
'/usr/local/lib/python3.4/site-packages/pip-6.0.8-py3.4.egg',
'/User/aaa/Documents/workspace/programming-python',... ]
This is what a truncated version of my programming-python directory looks like:
❯ tree
.
├── PP4E
│ ├── __init__.py
│ ├── Gui
│ │ ├── Tools
│ │ │ ├── __init__.py
│ │ │ └── widgets.py
│ │ └── __init__.py
│ ├── Lang
│ │ └── Calculator
│ │ ├── __init__.py
│ │ └── calc0.py
└── site-packages
└── PP4E.pth
The error message I'm getting is:
❯ python3 calc0.py
Traceback (most recent call last):
File "calc0.py", line 2, in <module>
from PP4E.Gui.Tools.widgets import frame, button, entry
ImportError: No module named 'PP4E'
Does anybody know what I have to do to get Python to find the PP4E module? Thanks.
I had my PYTHONPATH wrong -- the root directory is /Users, not /User. Thanks.