Unable to find module after import
up vote
1
down vote
favorite
I'm runnin Python 3.6.3 and I have the following module in a subdirectory that I'm trying to install through pip.
/g_plotter
setup.py
/g_plotter
__init__.py
g_plotter.py
Gparser.py
setup.py
from setuptools import setup
setup(
name='g_plotter',
packages=['g_plotter'],
include_package_data=True,
install_requires=[
'flask',
],
)
I installed that module form Docker in my container:
RUN pip3 install ./g_plotter
Then in my app code:
import g_plotter
print(dir(g_plotter))
which outputs
server_1 | ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
so using this import instead:
from g_plotter import g_plotter
results in
server_1 | Traceback (most recent call last):
server_1 | File "./g_server.py", line 21, in <module>
server_1 | from g_plotter import g_plotter
server_1 | File "/usr/local/lib/python3.7/site-packages/g_plotter/g_plotter.py", line 7, in <module>
server_1 | import Gparser
server_1 | ModuleNotFoundError: No module named 'Gparser'
When I run the submodule on it's own (It's a flask app), it works.
python python-3.x flask python-module
add a comment |
up vote
1
down vote
favorite
I'm runnin Python 3.6.3 and I have the following module in a subdirectory that I'm trying to install through pip.
/g_plotter
setup.py
/g_plotter
__init__.py
g_plotter.py
Gparser.py
setup.py
from setuptools import setup
setup(
name='g_plotter',
packages=['g_plotter'],
include_package_data=True,
install_requires=[
'flask',
],
)
I installed that module form Docker in my container:
RUN pip3 install ./g_plotter
Then in my app code:
import g_plotter
print(dir(g_plotter))
which outputs
server_1 | ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
so using this import instead:
from g_plotter import g_plotter
results in
server_1 | Traceback (most recent call last):
server_1 | File "./g_server.py", line 21, in <module>
server_1 | from g_plotter import g_plotter
server_1 | File "/usr/local/lib/python3.7/site-packages/g_plotter/g_plotter.py", line 7, in <module>
server_1 | import Gparser
server_1 | ModuleNotFoundError: No module named 'Gparser'
When I run the submodule on it's own (It's a flask app), it works.
python python-3.x flask python-module
Your structure is wrong. For example to importGparser
, you should writefrom g_plotter import Gparser
as it is in a module.
– Sraw
Nov 9 at 1:53
@Sraw, then in the flask app when running it directly I get "ImportError: cannot import name 'Gparser' from 'g_plotter'". And in my main app, it also errors out with "ModuleNotFoundError: No module named 'Gparser'"
– user994165
Nov 9 at 1:57
This is because your design is wrong... It's a little bit hard to explain in comment. But basically you can image that when you directly run your package inside the package, the directories structure doesn't contain your package top directory. But when you use it as a package, the directories structure will contain your package's directory. I don't know if it is clear enough... Essentially is package has an additional wrapper directory.
– Sraw
Nov 9 at 2:01
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm runnin Python 3.6.3 and I have the following module in a subdirectory that I'm trying to install through pip.
/g_plotter
setup.py
/g_plotter
__init__.py
g_plotter.py
Gparser.py
setup.py
from setuptools import setup
setup(
name='g_plotter',
packages=['g_plotter'],
include_package_data=True,
install_requires=[
'flask',
],
)
I installed that module form Docker in my container:
RUN pip3 install ./g_plotter
Then in my app code:
import g_plotter
print(dir(g_plotter))
which outputs
server_1 | ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
so using this import instead:
from g_plotter import g_plotter
results in
server_1 | Traceback (most recent call last):
server_1 | File "./g_server.py", line 21, in <module>
server_1 | from g_plotter import g_plotter
server_1 | File "/usr/local/lib/python3.7/site-packages/g_plotter/g_plotter.py", line 7, in <module>
server_1 | import Gparser
server_1 | ModuleNotFoundError: No module named 'Gparser'
When I run the submodule on it's own (It's a flask app), it works.
python python-3.x flask python-module
I'm runnin Python 3.6.3 and I have the following module in a subdirectory that I'm trying to install through pip.
/g_plotter
setup.py
/g_plotter
__init__.py
g_plotter.py
Gparser.py
setup.py
from setuptools import setup
setup(
name='g_plotter',
packages=['g_plotter'],
include_package_data=True,
install_requires=[
'flask',
],
)
I installed that module form Docker in my container:
RUN pip3 install ./g_plotter
Then in my app code:
import g_plotter
print(dir(g_plotter))
which outputs
server_1 | ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
so using this import instead:
from g_plotter import g_plotter
results in
server_1 | Traceback (most recent call last):
server_1 | File "./g_server.py", line 21, in <module>
server_1 | from g_plotter import g_plotter
server_1 | File "/usr/local/lib/python3.7/site-packages/g_plotter/g_plotter.py", line 7, in <module>
server_1 | import Gparser
server_1 | ModuleNotFoundError: No module named 'Gparser'
When I run the submodule on it's own (It's a flask app), it works.
python python-3.x flask python-module
python python-3.x flask python-module
asked Nov 9 at 1:46
user994165
3,6331763106
3,6331763106
Your structure is wrong. For example to importGparser
, you should writefrom g_plotter import Gparser
as it is in a module.
– Sraw
Nov 9 at 1:53
@Sraw, then in the flask app when running it directly I get "ImportError: cannot import name 'Gparser' from 'g_plotter'". And in my main app, it also errors out with "ModuleNotFoundError: No module named 'Gparser'"
– user994165
Nov 9 at 1:57
This is because your design is wrong... It's a little bit hard to explain in comment. But basically you can image that when you directly run your package inside the package, the directories structure doesn't contain your package top directory. But when you use it as a package, the directories structure will contain your package's directory. I don't know if it is clear enough... Essentially is package has an additional wrapper directory.
– Sraw
Nov 9 at 2:01
add a comment |
Your structure is wrong. For example to importGparser
, you should writefrom g_plotter import Gparser
as it is in a module.
– Sraw
Nov 9 at 1:53
@Sraw, then in the flask app when running it directly I get "ImportError: cannot import name 'Gparser' from 'g_plotter'". And in my main app, it also errors out with "ModuleNotFoundError: No module named 'Gparser'"
– user994165
Nov 9 at 1:57
This is because your design is wrong... It's a little bit hard to explain in comment. But basically you can image that when you directly run your package inside the package, the directories structure doesn't contain your package top directory. But when you use it as a package, the directories structure will contain your package's directory. I don't know if it is clear enough... Essentially is package has an additional wrapper directory.
– Sraw
Nov 9 at 2:01
Your structure is wrong. For example to import
Gparser
, you should write from g_plotter import Gparser
as it is in a module.– Sraw
Nov 9 at 1:53
Your structure is wrong. For example to import
Gparser
, you should write from g_plotter import Gparser
as it is in a module.– Sraw
Nov 9 at 1:53
@Sraw, then in the flask app when running it directly I get "ImportError: cannot import name 'Gparser' from 'g_plotter'". And in my main app, it also errors out with "ModuleNotFoundError: No module named 'Gparser'"
– user994165
Nov 9 at 1:57
@Sraw, then in the flask app when running it directly I get "ImportError: cannot import name 'Gparser' from 'g_plotter'". And in my main app, it also errors out with "ModuleNotFoundError: No module named 'Gparser'"
– user994165
Nov 9 at 1:57
This is because your design is wrong... It's a little bit hard to explain in comment. But basically you can image that when you directly run your package inside the package, the directories structure doesn't contain your package top directory. But when you use it as a package, the directories structure will contain your package's directory. I don't know if it is clear enough... Essentially is package has an additional wrapper directory.
– Sraw
Nov 9 at 2:01
This is because your design is wrong... It's a little bit hard to explain in comment. But basically you can image that when you directly run your package inside the package, the directories structure doesn't contain your package top directory. But when you use it as a package, the directories structure will contain your package's directory. I don't know if it is clear enough... Essentially is package has an additional wrapper directory.
– Sraw
Nov 9 at 2:01
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You must use absolute import in python 3, import Gparser
is not allowed any more. You can change it to:
from . import Gparser
from g_plotter import Gparser
Let you more clear, I will describe what do they mean.
import Gparser
Gparser = load_module()
sys.modules['Gparser'] = Gparser
from g_plotter import Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
from . import Gparser
package = find_package(__name__) # 'g_plotter'
Gparser_name = package + 'Gparser' # g_plotter.Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
Now you can understand, if you run g_plotter directly, actually __name__
is __main__
, so python can't find a package from it. Only if you import this submodule in other module, from . import something
can be worked.
these aren't working for me. Running the submodule directly from the command line gives me: " from . import Gparser ImportError: cannot import name 'Gparser"
– user994165
Nov 9 at 2:01
@user994165 You can't usefrom . import something
in main module. This statement must be used in one package.
– Yang HG
Nov 9 at 2:03
OK. I moved the submodule script to the root level and changed the import to "from g_plotter import Gparser", and used the same import in the app.
– user994165
Nov 9 at 21:52
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You must use absolute import in python 3, import Gparser
is not allowed any more. You can change it to:
from . import Gparser
from g_plotter import Gparser
Let you more clear, I will describe what do they mean.
import Gparser
Gparser = load_module()
sys.modules['Gparser'] = Gparser
from g_plotter import Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
from . import Gparser
package = find_package(__name__) # 'g_plotter'
Gparser_name = package + 'Gparser' # g_plotter.Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
Now you can understand, if you run g_plotter directly, actually __name__
is __main__
, so python can't find a package from it. Only if you import this submodule in other module, from . import something
can be worked.
these aren't working for me. Running the submodule directly from the command line gives me: " from . import Gparser ImportError: cannot import name 'Gparser"
– user994165
Nov 9 at 2:01
@user994165 You can't usefrom . import something
in main module. This statement must be used in one package.
– Yang HG
Nov 9 at 2:03
OK. I moved the submodule script to the root level and changed the import to "from g_plotter import Gparser", and used the same import in the app.
– user994165
Nov 9 at 21:52
add a comment |
up vote
1
down vote
accepted
You must use absolute import in python 3, import Gparser
is not allowed any more. You can change it to:
from . import Gparser
from g_plotter import Gparser
Let you more clear, I will describe what do they mean.
import Gparser
Gparser = load_module()
sys.modules['Gparser'] = Gparser
from g_plotter import Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
from . import Gparser
package = find_package(__name__) # 'g_plotter'
Gparser_name = package + 'Gparser' # g_plotter.Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
Now you can understand, if you run g_plotter directly, actually __name__
is __main__
, so python can't find a package from it. Only if you import this submodule in other module, from . import something
can be worked.
these aren't working for me. Running the submodule directly from the command line gives me: " from . import Gparser ImportError: cannot import name 'Gparser"
– user994165
Nov 9 at 2:01
@user994165 You can't usefrom . import something
in main module. This statement must be used in one package.
– Yang HG
Nov 9 at 2:03
OK. I moved the submodule script to the root level and changed the import to "from g_plotter import Gparser", and used the same import in the app.
– user994165
Nov 9 at 21:52
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You must use absolute import in python 3, import Gparser
is not allowed any more. You can change it to:
from . import Gparser
from g_plotter import Gparser
Let you more clear, I will describe what do they mean.
import Gparser
Gparser = load_module()
sys.modules['Gparser'] = Gparser
from g_plotter import Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
from . import Gparser
package = find_package(__name__) # 'g_plotter'
Gparser_name = package + 'Gparser' # g_plotter.Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
Now you can understand, if you run g_plotter directly, actually __name__
is __main__
, so python can't find a package from it. Only if you import this submodule in other module, from . import something
can be worked.
You must use absolute import in python 3, import Gparser
is not allowed any more. You can change it to:
from . import Gparser
from g_plotter import Gparser
Let you more clear, I will describe what do they mean.
import Gparser
Gparser = load_module()
sys.modules['Gparser'] = Gparser
from g_plotter import Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
from . import Gparser
package = find_package(__name__) # 'g_plotter'
Gparser_name = package + 'Gparser' # g_plotter.Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser
Now you can understand, if you run g_plotter directly, actually __name__
is __main__
, so python can't find a package from it. Only if you import this submodule in other module, from . import something
can be worked.
edited Nov 9 at 2:47
answered Nov 9 at 1:55
Yang HG
433
433
these aren't working for me. Running the submodule directly from the command line gives me: " from . import Gparser ImportError: cannot import name 'Gparser"
– user994165
Nov 9 at 2:01
@user994165 You can't usefrom . import something
in main module. This statement must be used in one package.
– Yang HG
Nov 9 at 2:03
OK. I moved the submodule script to the root level and changed the import to "from g_plotter import Gparser", and used the same import in the app.
– user994165
Nov 9 at 21:52
add a comment |
these aren't working for me. Running the submodule directly from the command line gives me: " from . import Gparser ImportError: cannot import name 'Gparser"
– user994165
Nov 9 at 2:01
@user994165 You can't usefrom . import something
in main module. This statement must be used in one package.
– Yang HG
Nov 9 at 2:03
OK. I moved the submodule script to the root level and changed the import to "from g_plotter import Gparser", and used the same import in the app.
– user994165
Nov 9 at 21:52
these aren't working for me. Running the submodule directly from the command line gives me: " from . import Gparser ImportError: cannot import name 'Gparser"
– user994165
Nov 9 at 2:01
these aren't working for me. Running the submodule directly from the command line gives me: " from . import Gparser ImportError: cannot import name 'Gparser"
– user994165
Nov 9 at 2:01
@user994165 You can't use
from . import something
in main module. This statement must be used in one package.– Yang HG
Nov 9 at 2:03
@user994165 You can't use
from . import something
in main module. This statement must be used in one package.– Yang HG
Nov 9 at 2:03
OK. I moved the submodule script to the root level and changed the import to "from g_plotter import Gparser", and used the same import in the app.
– user994165
Nov 9 at 21:52
OK. I moved the submodule script to the root level and changed the import to "from g_plotter import Gparser", and used the same import in the app.
– user994165
Nov 9 at 21:52
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53218689%2funable-to-find-module-after-import%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
Your structure is wrong. For example to import
Gparser
, you should writefrom g_plotter import Gparser
as it is in a module.– Sraw
Nov 9 at 1:53
@Sraw, then in the flask app when running it directly I get "ImportError: cannot import name 'Gparser' from 'g_plotter'". And in my main app, it also errors out with "ModuleNotFoundError: No module named 'Gparser'"
– user994165
Nov 9 at 1:57
This is because your design is wrong... It's a little bit hard to explain in comment. But basically you can image that when you directly run your package inside the package, the directories structure doesn't contain your package top directory. But when you use it as a package, the directories structure will contain your package's directory. I don't know if it is clear enough... Essentially is package has an additional wrapper directory.
– Sraw
Nov 9 at 2:01