0

I have a Project where there is a python(.py) file inside a Directory say A, under the folder A , I have a file having the code:

from utils.utils import csv_file_transform

where utils is a sibling directory to A

 /A
  file.py

 /utils
    __init__.py
    utils.py

but, I am getting a error No Module named utils.utils, utils is not a package I tried adding the path using sys, but its still giving the same error

import sys
sys.path.append('C:/Users/Sri/Documents/newmod/folder/utils')

The code is working fine in ubuntu but not in windows, I am using a windows system

SriHarsha
  • 23
  • 1
  • 7

1 Answers1

0

as much as I don't support link only / reference only answers I reckon that your question is answered best by referencing the existing discussions and providing broader context on Python importing mechanism.

Your Requirements

  • Are you actually working on creating on Python module? If this is the case, it would be good if you could establish a rigorous structure for your module and follow those guidelines.

  • Consider the following article by Jason C. McDonald on strutting a hypothetical module omission in Python. Given the following structure:

       omission-git
       ├── LICENSE.md
       ├── omission
       │   ├── app.py
       │   ├── common
       │   │   ├── classproperty.py
       │   │   ├── constants.py
       │   │   ├── game_enums.py
       │   │   └── __init__.py
       │   ├── data
       │   │   ├── data_loader.py
       │   │   ├── game_round_settings.py
       │   │   ├── __init__.py
       │   │   ├── scoreboard.py
       │   │   └── settings.py
       │   ├── game
       │   │   ├── content_loader.py
       │   │   ├── game_item.py
       │   │   ├── game_round.py
       │   │   ├── __init__.py
       │   │   └── timer.py
       │   ├── __init__.py
       │   ├── __main__.py
       │   ├── resources
       │   └── tests
       │       ├── __init__.py
       │       ├── test_game_item.py
       │       ├── test_game_round_settings.py
       │       ├── test_scoreboard.py
       │       ├── test_settings.py
       │       ├── test_test.py
       │       └── test_timer.py
       ├── pylintrc
       ├── README.md
       └── .gitignore
    

    Jason would import the modules in a following manner from omission.common.game_enums import GameMode.

Relative Imports

@am5 suggests adding

import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

to your __init__.py files. If you study the related discussion you will observer that views on modifying sys.path are diverse. Some may argue that failed imports and errors like:

ImportError: attempted relative import with no known parent package

are actually indication of code smell. As a solution, which I hope you won't find patronising, I would suggest that you solve your importing challenge by:

a) Deciding on your requirements on how "deep" you want to go into modular architecture. b) Adopting a corresponding recommended architecture with rigorous approach to module structure c) Refraining from hard-coding any path via sys.path. Appending relative paths, as outlined above, is not a flawless solution but has some merits and may be worth considering.

Other worthy discussions/artefacts

Konrad
  • 17,740
  • 16
  • 106
  • 167