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.










share|improve this question





















  • 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










  • 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














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.










share|improve this question





















  • 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










  • 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












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 1:46









user994165

3,6331763106




3,6331763106











  • 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










  • 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










  • @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












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.






share|improve this answer






















  • 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










  • 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










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',
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
);



);













draft saved

draft discarded


















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

























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.






share|improve this answer






















  • 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










  • 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














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.






share|improve this answer






















  • 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










  • 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












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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
















  • 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










  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế