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:
- How can I upload my code with importing files within the package using absolute import? or
- 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 *