0

I created a GUI app with following file structure:

MyApp ├──
      ├── LICENSE
      ├── README.md
      ├── setup.cfg
      ├── setup.py
      └── MyApp
          ├── data
          │   ├── dataset1.txt
          │   └── dataset2.txt
          ├── Controller.py
          ├── __init__.py
          ├── Model.py
          ├── MyApp.py
          └── View.py

I uploaded it to Pypi and install it on my computer to check if it is working. But when I run it I got the following error:

ModuleNotFoundError: No module named 'View'

I managed to solve the problem by relatively importing files within the package. Something like this:

from .View import *

Everything worked fine after installation. So, uploading the file to Pypi was successful. But when I tried to edit the local code to release new update, I got the following error:

ImportError: attempted relative import with no known parent package

Note that I got this error trying to edit the local code on my working space, not the installed one. So now every time I want to edit my code on my computer I have to change relative import to absolute import and when I want to upload a new release to Pypi, in order to the installed version works fine, I have to change absolute import to relative import.

So logically there must be two solution to my problem:

  1. How can I upload my code with importing files within the package using absolute import? or
  2. How can I work on my package locally using relative import?

The app is run on command line by typing its name. Here is my setup file:

import os
from setuptools import *

# The directory containing this file
HERE = os.path.abspath(os.path.dirname(__file__))

# The text of the README file
with open(os.path.join(HERE, "README.md")) as fid:
    README = fid.read()

setup(
    name="MyApp",
    version="v0.1",
    description="My description",
    long_description=README,
    long_description_content_type="text/markdown",
    author="My name",
    author_email="MyEmailAddress@mail.com",
    license="MIT",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3.6"
    ],
    packages=find_packages(),
    package_dir={"MyApp": "MyApp"},
    package_data={
        "MyApp": [
            "data/*.txt"]
    },
    entry_points={
        "console_scripts": [
            "MyApp=MyApp.MyApp:MyApp",
        ]
    }
)

Here is the content of __init__.py:

from .MyApp import *
Asdoost
  • 316
  • 1
  • 2
  • 15
  • Does this answer your question? [Attempted relative import with no known parent package](https://stackoverflow.com/questions/55084977/attempted-relative-import-with-no-known-parent-package) – Maurice Meyer Feb 03 '22 at 16:44
  • No. The problem is the different behavior. When I use relative import I could install the package without problem but I face Import error when I try to modify the code on my computer. When I use absolute import and install the package I face with ModuleNotFoundError but I could modify the code without ImportError. – Asdoost Feb 03 '22 at 16:52
  • Show your `setup.py` and/or `setup.cfg` maybe. -- From which file are you trying to do the import and which command do you run from which current working directory? -- https://stackoverflow.com/a/61698924 – sinoroc Feb 03 '22 at 17:46
  • I don't think the problem is in setup file. If it was it didn't work with relative import. As I showed above one of them is "from .Model import Model" in app file. Another one is "from .Controller import Controller" in "View.py". If you are looking for conflict it is not that. The problem is relative import. – Asdoost Feb 03 '22 at 18:12
  • Is there something called `Model` (a class maybe?) in `Model.py`? And something called `Controller` in `Controller.py`? – sinoroc Feb 03 '22 at 22:21
  • How are you running the code locally? – Mr_and_Mrs_D Feb 04 '22 at 11:57
  • It's GUI app. It runs by typing its name in terminal. In regard of @sinoroc's question, no there is not such a conflict. – Asdoost Feb 04 '22 at 12:29
  • when you say "trying to modify the code on my working space, not the installed one". - how _exactly_ are you running your code? From which directory and which file? – Mr_and_Mrs_D Feb 05 '22 at 19:44
  • Sorry I'm replying with delay. I run the code from home directory on Linux. I would be appreciated if you vote the question in order to more users see the question. – Asdoost Feb 06 '22 at 12:21

1 Answers1

0

You can run your app locally with the -m switch from outside the app directory. This allows to resolve relative imports correctly:

path/to/MyApp$ python -m MyApp.MyApp
a_guest
  • 34,165
  • 12
  • 64
  • 118
  • No. It doesn't solve the problem. I still get the `ImportError: attempted relative import with no known parent package` . – Asdoost Feb 07 '22 at 15:21
  • @Asdoost How exactly do you run your program? Please add the exact command used as well as the directory from which it is executed. – a_guest Feb 07 '22 at 15:47