Python/Flask - suddenly getting “ImportError: No module named…” error
I'm running into what I believe is a fairly common problem in Python apps - ImportError: No module named lib.handle_data
- but I'm not experienced enough with the Python/Flask ecosystem to really understand why this is happening, especially since my code was working until I made a small adjustment and reinstalled some dependencies. Ok here's how it went down...
I'm working on a Flask app and, up until a little while ago, it was running fine. I realized that I hadn't included semantic versioning for the dependencies in my setup.py
file. Originally, the file looked like this:
from setuptools import setup
setup(
name='my_app',
packages=['my_app'],
include_package_data=True,
python_requires='>3.6.4',
install_requires=[
'flask',
'gtfs-realtime-bindings',
'gunicorn',
],
)
which I changed to look like:
from setuptools import setup
setup(
name='my_app',
packages=['my_app'],
include_package_data=True,
python_requires='>3.6.4',
install_requires=[
'flask==0.12.2',
'gtfs-realtime-bindings==0.0.5',
'gunicorn==19.7.1',
],
)
and then just to make sure all was well, I ran python setup.py install
and everything seemed to go ok - none of the versions changed, as these were what had already been installed.
However, when I ran flask run
, the server wouldn't start and I was left with the error:
import my_app.lib.handle_data as HandleData
ImportError: No module named lib.handle_data
and trying to start the server with gunicorn via
gunicorn my_app.my_app:app -b 0.0.0.0:8000
yields the same error (no surprise, I'm sure, just being comprehensive).
Here's the file structure:
setup.py
my_app/
|
- __pycache__/
- __init__.py
- my_app.py
- lib/
|
- __pycache__/
- __init__.py
- handle_data.py
- request.py
The contents of my_app/__init__.py
are:
from .my_app import app
and my_app/lib/__init__.py
is empty.
How did I break my app, and how can I fix this?
Python version is 3.6.4, running this on macOS Mojave, 10.14. And if I didn't make this clear, my app was running just fine before I made this change/reinstalled deps.
python python-3.x flask
add a comment |
I'm running into what I believe is a fairly common problem in Python apps - ImportError: No module named lib.handle_data
- but I'm not experienced enough with the Python/Flask ecosystem to really understand why this is happening, especially since my code was working until I made a small adjustment and reinstalled some dependencies. Ok here's how it went down...
I'm working on a Flask app and, up until a little while ago, it was running fine. I realized that I hadn't included semantic versioning for the dependencies in my setup.py
file. Originally, the file looked like this:
from setuptools import setup
setup(
name='my_app',
packages=['my_app'],
include_package_data=True,
python_requires='>3.6.4',
install_requires=[
'flask',
'gtfs-realtime-bindings',
'gunicorn',
],
)
which I changed to look like:
from setuptools import setup
setup(
name='my_app',
packages=['my_app'],
include_package_data=True,
python_requires='>3.6.4',
install_requires=[
'flask==0.12.2',
'gtfs-realtime-bindings==0.0.5',
'gunicorn==19.7.1',
],
)
and then just to make sure all was well, I ran python setup.py install
and everything seemed to go ok - none of the versions changed, as these were what had already been installed.
However, when I ran flask run
, the server wouldn't start and I was left with the error:
import my_app.lib.handle_data as HandleData
ImportError: No module named lib.handle_data
and trying to start the server with gunicorn via
gunicorn my_app.my_app:app -b 0.0.0.0:8000
yields the same error (no surprise, I'm sure, just being comprehensive).
Here's the file structure:
setup.py
my_app/
|
- __pycache__/
- __init__.py
- my_app.py
- lib/
|
- __pycache__/
- __init__.py
- handle_data.py
- request.py
The contents of my_app/__init__.py
are:
from .my_app import app
and my_app/lib/__init__.py
is empty.
How did I break my app, and how can I fix this?
Python version is 3.6.4, running this on macOS Mojave, 10.14. And if I didn't make this clear, my app was running just fine before I made this change/reinstalled deps.
python python-3.x flask
4
Not sure why it worked before, but you should probably add"my_app.lib"
topackages
.
– a_guest
Jan 1 at 2:03
@a_guest Huh! Yes, that fixed it! If you'd please submit that as an answer, I'll accept it.
– skwidbreth
Jan 1 at 2:17
add a comment |
I'm running into what I believe is a fairly common problem in Python apps - ImportError: No module named lib.handle_data
- but I'm not experienced enough with the Python/Flask ecosystem to really understand why this is happening, especially since my code was working until I made a small adjustment and reinstalled some dependencies. Ok here's how it went down...
I'm working on a Flask app and, up until a little while ago, it was running fine. I realized that I hadn't included semantic versioning for the dependencies in my setup.py
file. Originally, the file looked like this:
from setuptools import setup
setup(
name='my_app',
packages=['my_app'],
include_package_data=True,
python_requires='>3.6.4',
install_requires=[
'flask',
'gtfs-realtime-bindings',
'gunicorn',
],
)
which I changed to look like:
from setuptools import setup
setup(
name='my_app',
packages=['my_app'],
include_package_data=True,
python_requires='>3.6.4',
install_requires=[
'flask==0.12.2',
'gtfs-realtime-bindings==0.0.5',
'gunicorn==19.7.1',
],
)
and then just to make sure all was well, I ran python setup.py install
and everything seemed to go ok - none of the versions changed, as these were what had already been installed.
However, when I ran flask run
, the server wouldn't start and I was left with the error:
import my_app.lib.handle_data as HandleData
ImportError: No module named lib.handle_data
and trying to start the server with gunicorn via
gunicorn my_app.my_app:app -b 0.0.0.0:8000
yields the same error (no surprise, I'm sure, just being comprehensive).
Here's the file structure:
setup.py
my_app/
|
- __pycache__/
- __init__.py
- my_app.py
- lib/
|
- __pycache__/
- __init__.py
- handle_data.py
- request.py
The contents of my_app/__init__.py
are:
from .my_app import app
and my_app/lib/__init__.py
is empty.
How did I break my app, and how can I fix this?
Python version is 3.6.4, running this on macOS Mojave, 10.14. And if I didn't make this clear, my app was running just fine before I made this change/reinstalled deps.
python python-3.x flask
I'm running into what I believe is a fairly common problem in Python apps - ImportError: No module named lib.handle_data
- but I'm not experienced enough with the Python/Flask ecosystem to really understand why this is happening, especially since my code was working until I made a small adjustment and reinstalled some dependencies. Ok here's how it went down...
I'm working on a Flask app and, up until a little while ago, it was running fine. I realized that I hadn't included semantic versioning for the dependencies in my setup.py
file. Originally, the file looked like this:
from setuptools import setup
setup(
name='my_app',
packages=['my_app'],
include_package_data=True,
python_requires='>3.6.4',
install_requires=[
'flask',
'gtfs-realtime-bindings',
'gunicorn',
],
)
which I changed to look like:
from setuptools import setup
setup(
name='my_app',
packages=['my_app'],
include_package_data=True,
python_requires='>3.6.4',
install_requires=[
'flask==0.12.2',
'gtfs-realtime-bindings==0.0.5',
'gunicorn==19.7.1',
],
)
and then just to make sure all was well, I ran python setup.py install
and everything seemed to go ok - none of the versions changed, as these were what had already been installed.
However, when I ran flask run
, the server wouldn't start and I was left with the error:
import my_app.lib.handle_data as HandleData
ImportError: No module named lib.handle_data
and trying to start the server with gunicorn via
gunicorn my_app.my_app:app -b 0.0.0.0:8000
yields the same error (no surprise, I'm sure, just being comprehensive).
Here's the file structure:
setup.py
my_app/
|
- __pycache__/
- __init__.py
- my_app.py
- lib/
|
- __pycache__/
- __init__.py
- handle_data.py
- request.py
The contents of my_app/__init__.py
are:
from .my_app import app
and my_app/lib/__init__.py
is empty.
How did I break my app, and how can I fix this?
Python version is 3.6.4, running this on macOS Mojave, 10.14. And if I didn't make this clear, my app was running just fine before I made this change/reinstalled deps.
python python-3.x flask
python python-3.x flask
asked Jan 1 at 1:42
skwidbrethskwidbreth
2,48511655
2,48511655
4
Not sure why it worked before, but you should probably add"my_app.lib"
topackages
.
– a_guest
Jan 1 at 2:03
@a_guest Huh! Yes, that fixed it! If you'd please submit that as an answer, I'll accept it.
– skwidbreth
Jan 1 at 2:17
add a comment |
4
Not sure why it worked before, but you should probably add"my_app.lib"
topackages
.
– a_guest
Jan 1 at 2:03
@a_guest Huh! Yes, that fixed it! If you'd please submit that as an answer, I'll accept it.
– skwidbreth
Jan 1 at 2:17
4
4
Not sure why it worked before, but you should probably add
"my_app.lib"
to packages
.– a_guest
Jan 1 at 2:03
Not sure why it worked before, but you should probably add
"my_app.lib"
to packages
.– a_guest
Jan 1 at 2:03
@a_guest Huh! Yes, that fixed it! If you'd please submit that as an answer, I'll accept it.
– skwidbreth
Jan 1 at 2:17
@a_guest Huh! Yes, that fixed it! If you'd please submit that as an answer, I'll accept it.
– skwidbreth
Jan 1 at 2:17
add a comment |
2 Answers
2
active
oldest
votes
You should add "my_app.lib"
to packages
because otherwise the corresponding sub-package and the my_app.lib.*
modules won't be installed. You can check the existing installation by
>>> import my_app
>>> my_app.__file__
'/path/to/virtualenv/lib/python3.6/site-packages/my_app/__init__.py'
Now inspecting the corresponding directory will reveal that lib
is missing. If you add "my_app.lib"
to packages
then the lib
directory (package) will be installed as well.
As to why it worked before while you didn't change anything about the package setup / structure I can only speculate. There is however one thing about the installation process itself that can have an effect. If you had installed the package in --editable
(-e
) mode (or python setup.py develop
)1 before then your environment would contain a my-app.egg-link
pointing to the package's source directory instead of a copy of the relevant files. This way the my_app.lib
is of course not missing (as it's contained in the source directory). According to your description you did a "normal" installation after applying the changes and this requires the full structure as specified in packages
to be complete (also using --upgrade
would remove the link).
1. --editable / -e
is the pip
option to place a link to the source directory rather than to copy the relevant files; using setup.py
directly the corresponding mode is develop
, i.e. python setup.py develop
.
Terrific, thank you.
– skwidbreth
Jan 1 at 14:15
add a comment |
this question was already answered to anyone else that stumbles upon this creds to a_guest. Just trying to save other people's time btw a_guest if u want the reputation from me posting this I will delete it and u can post one I just didn't want someone to spend their time on an answered Q
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992552%2fpython-flask-suddenly-getting-importerror-no-module-named-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should add "my_app.lib"
to packages
because otherwise the corresponding sub-package and the my_app.lib.*
modules won't be installed. You can check the existing installation by
>>> import my_app
>>> my_app.__file__
'/path/to/virtualenv/lib/python3.6/site-packages/my_app/__init__.py'
Now inspecting the corresponding directory will reveal that lib
is missing. If you add "my_app.lib"
to packages
then the lib
directory (package) will be installed as well.
As to why it worked before while you didn't change anything about the package setup / structure I can only speculate. There is however one thing about the installation process itself that can have an effect. If you had installed the package in --editable
(-e
) mode (or python setup.py develop
)1 before then your environment would contain a my-app.egg-link
pointing to the package's source directory instead of a copy of the relevant files. This way the my_app.lib
is of course not missing (as it's contained in the source directory). According to your description you did a "normal" installation after applying the changes and this requires the full structure as specified in packages
to be complete (also using --upgrade
would remove the link).
1. --editable / -e
is the pip
option to place a link to the source directory rather than to copy the relevant files; using setup.py
directly the corresponding mode is develop
, i.e. python setup.py develop
.
Terrific, thank you.
– skwidbreth
Jan 1 at 14:15
add a comment |
You should add "my_app.lib"
to packages
because otherwise the corresponding sub-package and the my_app.lib.*
modules won't be installed. You can check the existing installation by
>>> import my_app
>>> my_app.__file__
'/path/to/virtualenv/lib/python3.6/site-packages/my_app/__init__.py'
Now inspecting the corresponding directory will reveal that lib
is missing. If you add "my_app.lib"
to packages
then the lib
directory (package) will be installed as well.
As to why it worked before while you didn't change anything about the package setup / structure I can only speculate. There is however one thing about the installation process itself that can have an effect. If you had installed the package in --editable
(-e
) mode (or python setup.py develop
)1 before then your environment would contain a my-app.egg-link
pointing to the package's source directory instead of a copy of the relevant files. This way the my_app.lib
is of course not missing (as it's contained in the source directory). According to your description you did a "normal" installation after applying the changes and this requires the full structure as specified in packages
to be complete (also using --upgrade
would remove the link).
1. --editable / -e
is the pip
option to place a link to the source directory rather than to copy the relevant files; using setup.py
directly the corresponding mode is develop
, i.e. python setup.py develop
.
Terrific, thank you.
– skwidbreth
Jan 1 at 14:15
add a comment |
You should add "my_app.lib"
to packages
because otherwise the corresponding sub-package and the my_app.lib.*
modules won't be installed. You can check the existing installation by
>>> import my_app
>>> my_app.__file__
'/path/to/virtualenv/lib/python3.6/site-packages/my_app/__init__.py'
Now inspecting the corresponding directory will reveal that lib
is missing. If you add "my_app.lib"
to packages
then the lib
directory (package) will be installed as well.
As to why it worked before while you didn't change anything about the package setup / structure I can only speculate. There is however one thing about the installation process itself that can have an effect. If you had installed the package in --editable
(-e
) mode (or python setup.py develop
)1 before then your environment would contain a my-app.egg-link
pointing to the package's source directory instead of a copy of the relevant files. This way the my_app.lib
is of course not missing (as it's contained in the source directory). According to your description you did a "normal" installation after applying the changes and this requires the full structure as specified in packages
to be complete (also using --upgrade
would remove the link).
1. --editable / -e
is the pip
option to place a link to the source directory rather than to copy the relevant files; using setup.py
directly the corresponding mode is develop
, i.e. python setup.py develop
.
You should add "my_app.lib"
to packages
because otherwise the corresponding sub-package and the my_app.lib.*
modules won't be installed. You can check the existing installation by
>>> import my_app
>>> my_app.__file__
'/path/to/virtualenv/lib/python3.6/site-packages/my_app/__init__.py'
Now inspecting the corresponding directory will reveal that lib
is missing. If you add "my_app.lib"
to packages
then the lib
directory (package) will be installed as well.
As to why it worked before while you didn't change anything about the package setup / structure I can only speculate. There is however one thing about the installation process itself that can have an effect. If you had installed the package in --editable
(-e
) mode (or python setup.py develop
)1 before then your environment would contain a my-app.egg-link
pointing to the package's source directory instead of a copy of the relevant files. This way the my_app.lib
is of course not missing (as it's contained in the source directory). According to your description you did a "normal" installation after applying the changes and this requires the full structure as specified in packages
to be complete (also using --upgrade
would remove the link).
1. --editable / -e
is the pip
option to place a link to the source directory rather than to copy the relevant files; using setup.py
directly the corresponding mode is develop
, i.e. python setup.py develop
.
edited Jan 1 at 10:21
answered Jan 1 at 9:26
a_guesta_guest
6,62821241
6,62821241
Terrific, thank you.
– skwidbreth
Jan 1 at 14:15
add a comment |
Terrific, thank you.
– skwidbreth
Jan 1 at 14:15
Terrific, thank you.
– skwidbreth
Jan 1 at 14:15
Terrific, thank you.
– skwidbreth
Jan 1 at 14:15
add a comment |
this question was already answered to anyone else that stumbles upon this creds to a_guest. Just trying to save other people's time btw a_guest if u want the reputation from me posting this I will delete it and u can post one I just didn't want someone to spend their time on an answered Q
add a comment |
this question was already answered to anyone else that stumbles upon this creds to a_guest. Just trying to save other people's time btw a_guest if u want the reputation from me posting this I will delete it and u can post one I just didn't want someone to spend their time on an answered Q
add a comment |
this question was already answered to anyone else that stumbles upon this creds to a_guest. Just trying to save other people's time btw a_guest if u want the reputation from me posting this I will delete it and u can post one I just didn't want someone to spend their time on an answered Q
this question was already answered to anyone else that stumbles upon this creds to a_guest. Just trying to save other people's time btw a_guest if u want the reputation from me posting this I will delete it and u can post one I just didn't want someone to spend their time on an answered Q
answered Jan 1 at 2:35
RedPythonRedPython
485
485
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992552%2fpython-flask-suddenly-getting-importerror-no-module-named-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Not sure why it worked before, but you should probably add
"my_app.lib"
topackages
.– a_guest
Jan 1 at 2:03
@a_guest Huh! Yes, that fixed it! If you'd please submit that as an answer, I'll accept it.
– skwidbreth
Jan 1 at 2:17