Same code not working in PyCharm but working outside PyCharm





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to run a google ortools example in PyCharm and got the following errors:



/Users/edamame/project/ortools/simple_program.py
Traceback (most recent call last):
File "/Users/edamame/project/ortools/simple_program.py", line 3, in <module>
from ortools.linear_solver import pywraplp
ModuleNotFoundError: No module named 'ortools.linear_solver'

Process finished with exit code 1


However, if I run the same code outside the PyCharm (under the corresponding project venv), it works fine:



(venv) edamame$ pwd
/Users/edamame/project/ortools/
(venv) edamame$ python simple_program.py
Solution:
Objective value = 4.0
x = 1.0
y = 1.0


I then tried to print below in both cases:



import sys
print('n'.join(sys.path))


The outputs:



In PyCharm:



/Users/edamame/project/venv/bin/python
/Users/edamame/project/ortools/so_example.py
/Users/edamame/project/ortools
/Users/edamame/project
/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
/Users/edamame/project/venv/lib/python3.6/site-packages
/Users/edamame/project/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
/Users/edamame/project/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg


Outside PyCharm:



(venv) edamame$ python so_example.py 
/Users/edamame/project/ortools
/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
/Users/edamame/project/venv/lib/python3.6/site-packages
/Users/edamame/project/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
/Users/edamame/project/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg


They seem to be the same though ... not sure what could be the problem ...



How do I make the same code working in PyCharm?










share|improve this question































    0















    I am trying to run a google ortools example in PyCharm and got the following errors:



    /Users/edamame/project/ortools/simple_program.py
    Traceback (most recent call last):
    File "/Users/edamame/project/ortools/simple_program.py", line 3, in <module>
    from ortools.linear_solver import pywraplp
    ModuleNotFoundError: No module named 'ortools.linear_solver'

    Process finished with exit code 1


    However, if I run the same code outside the PyCharm (under the corresponding project venv), it works fine:



    (venv) edamame$ pwd
    /Users/edamame/project/ortools/
    (venv) edamame$ python simple_program.py
    Solution:
    Objective value = 4.0
    x = 1.0
    y = 1.0


    I then tried to print below in both cases:



    import sys
    print('n'.join(sys.path))


    The outputs:



    In PyCharm:



    /Users/edamame/project/venv/bin/python
    /Users/edamame/project/ortools/so_example.py
    /Users/edamame/project/ortools
    /Users/edamame/project
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
    /Users/edamame/project/venv/lib/python3.6/site-packages
    /Users/edamame/project/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
    /Users/edamame/project/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg


    Outside PyCharm:



    (venv) edamame$ python so_example.py 
    /Users/edamame/project/ortools
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
    /Users/edamame/project/venv/lib/python3.6/site-packages
    /Users/edamame/project/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
    /Users/edamame/project/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg


    They seem to be the same though ... not sure what could be the problem ...



    How do I make the same code working in PyCharm?










    share|improve this question



























      0












      0








      0








      I am trying to run a google ortools example in PyCharm and got the following errors:



      /Users/edamame/project/ortools/simple_program.py
      Traceback (most recent call last):
      File "/Users/edamame/project/ortools/simple_program.py", line 3, in <module>
      from ortools.linear_solver import pywraplp
      ModuleNotFoundError: No module named 'ortools.linear_solver'

      Process finished with exit code 1


      However, if I run the same code outside the PyCharm (under the corresponding project venv), it works fine:



      (venv) edamame$ pwd
      /Users/edamame/project/ortools/
      (venv) edamame$ python simple_program.py
      Solution:
      Objective value = 4.0
      x = 1.0
      y = 1.0


      I then tried to print below in both cases:



      import sys
      print('n'.join(sys.path))


      The outputs:



      In PyCharm:



      /Users/edamame/project/venv/bin/python
      /Users/edamame/project/ortools/so_example.py
      /Users/edamame/project/ortools
      /Users/edamame/project
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
      /Users/edamame/project/venv/lib/python3.6/site-packages
      /Users/edamame/project/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
      /Users/edamame/project/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg


      Outside PyCharm:



      (venv) edamame$ python so_example.py 
      /Users/edamame/project/ortools
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
      /Users/edamame/project/venv/lib/python3.6/site-packages
      /Users/edamame/project/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
      /Users/edamame/project/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg


      They seem to be the same though ... not sure what could be the problem ...



      How do I make the same code working in PyCharm?










      share|improve this question
















      I am trying to run a google ortools example in PyCharm and got the following errors:



      /Users/edamame/project/ortools/simple_program.py
      Traceback (most recent call last):
      File "/Users/edamame/project/ortools/simple_program.py", line 3, in <module>
      from ortools.linear_solver import pywraplp
      ModuleNotFoundError: No module named 'ortools.linear_solver'

      Process finished with exit code 1


      However, if I run the same code outside the PyCharm (under the corresponding project venv), it works fine:



      (venv) edamame$ pwd
      /Users/edamame/project/ortools/
      (venv) edamame$ python simple_program.py
      Solution:
      Objective value = 4.0
      x = 1.0
      y = 1.0


      I then tried to print below in both cases:



      import sys
      print('n'.join(sys.path))


      The outputs:



      In PyCharm:



      /Users/edamame/project/venv/bin/python
      /Users/edamame/project/ortools/so_example.py
      /Users/edamame/project/ortools
      /Users/edamame/project
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
      /Users/edamame/project/venv/lib/python3.6/site-packages
      /Users/edamame/project/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
      /Users/edamame/project/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg


      Outside PyCharm:



      (venv) edamame$ python so_example.py 
      /Users/edamame/project/ortools
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
      /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
      /Users/edamame/project/venv/lib/python3.6/site-packages
      /Users/edamame/project/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
      /Users/edamame/project/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg


      They seem to be the same though ... not sure what could be the problem ...



      How do I make the same code working in PyCharm?







      python-3.x pycharm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 5:59







      Edamame

















      asked Nov 21 '18 at 23:20









      EdamameEdamame

      4,9892385171




      4,9892385171
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You have a namespace collision between custom ortools package and the installed one. PyCharm adds the project root (/Users/edamame/project) to PYTHONPATH by default and it causes the problem.



          Either rename your custom ortools package or disable Add content roots to PYTHONPATH option in the dedicated Run Configuration in PYCharm.






          share|improve this answer































            1














            The env var PYTHONPATH is relevant. In both situations, print the value of sys.path:



            import sys
            print('n'.join(sys.path))


            In bash, you may find it helpful to enter the venv and then $ open /Applications/PyCharm.app (or whatever it's called on your Mac). Then the running pycharm will inherit PYTHONPATH from bash.



            Here is another approach. Within pycharm type CMD-, (command comma) for preferences, search for Project Structure, right click on the edamame/project folder, and choose alt-S Sources. This will add the folder to your path within pycharm. Again, use print() to verify sys.path contains what you expect it to contain.






            share|improve this answer
























            • I print the sys.path for both cases. It is long, so I added to the original questions.

              – Edamame
              Nov 22 '18 at 5:53











            • I hadn't realized you were following the developers.google.com/optimization/install/python/mac instructions. To adapt them to pycharm, try clicking CMD-comma and Add an Existing environment Project Interpreter which matches the bash output of which python. That is the usual way to copy a conda or pip venv into a Pycharm or Idea environment.

              – J_H
              Nov 22 '18 at 17:38














            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421810%2fsame-code-not-working-in-pycharm-but-working-outside-pycharm%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









            1














            You have a namespace collision between custom ortools package and the installed one. PyCharm adds the project root (/Users/edamame/project) to PYTHONPATH by default and it causes the problem.



            Either rename your custom ortools package or disable Add content roots to PYTHONPATH option in the dedicated Run Configuration in PYCharm.






            share|improve this answer




























              1














              You have a namespace collision between custom ortools package and the installed one. PyCharm adds the project root (/Users/edamame/project) to PYTHONPATH by default and it causes the problem.



              Either rename your custom ortools package or disable Add content roots to PYTHONPATH option in the dedicated Run Configuration in PYCharm.






              share|improve this answer


























                1












                1








                1







                You have a namespace collision between custom ortools package and the installed one. PyCharm adds the project root (/Users/edamame/project) to PYTHONPATH by default and it causes the problem.



                Either rename your custom ortools package or disable Add content roots to PYTHONPATH option in the dedicated Run Configuration in PYCharm.






                share|improve this answer













                You have a namespace collision between custom ortools package and the installed one. PyCharm adds the project root (/Users/edamame/project) to PYTHONPATH by default and it causes the problem.



                Either rename your custom ortools package or disable Add content roots to PYTHONPATH option in the dedicated Run Configuration in PYCharm.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 6:50









                Pavel KarateevPavel Karateev

                1,387914




                1,387914

























                    1














                    The env var PYTHONPATH is relevant. In both situations, print the value of sys.path:



                    import sys
                    print('n'.join(sys.path))


                    In bash, you may find it helpful to enter the venv and then $ open /Applications/PyCharm.app (or whatever it's called on your Mac). Then the running pycharm will inherit PYTHONPATH from bash.



                    Here is another approach. Within pycharm type CMD-, (command comma) for preferences, search for Project Structure, right click on the edamame/project folder, and choose alt-S Sources. This will add the folder to your path within pycharm. Again, use print() to verify sys.path contains what you expect it to contain.






                    share|improve this answer
























                    • I print the sys.path for both cases. It is long, so I added to the original questions.

                      – Edamame
                      Nov 22 '18 at 5:53











                    • I hadn't realized you were following the developers.google.com/optimization/install/python/mac instructions. To adapt them to pycharm, try clicking CMD-comma and Add an Existing environment Project Interpreter which matches the bash output of which python. That is the usual way to copy a conda or pip venv into a Pycharm or Idea environment.

                      – J_H
                      Nov 22 '18 at 17:38


















                    1














                    The env var PYTHONPATH is relevant. In both situations, print the value of sys.path:



                    import sys
                    print('n'.join(sys.path))


                    In bash, you may find it helpful to enter the venv and then $ open /Applications/PyCharm.app (or whatever it's called on your Mac). Then the running pycharm will inherit PYTHONPATH from bash.



                    Here is another approach. Within pycharm type CMD-, (command comma) for preferences, search for Project Structure, right click on the edamame/project folder, and choose alt-S Sources. This will add the folder to your path within pycharm. Again, use print() to verify sys.path contains what you expect it to contain.






                    share|improve this answer
























                    • I print the sys.path for both cases. It is long, so I added to the original questions.

                      – Edamame
                      Nov 22 '18 at 5:53











                    • I hadn't realized you were following the developers.google.com/optimization/install/python/mac instructions. To adapt them to pycharm, try clicking CMD-comma and Add an Existing environment Project Interpreter which matches the bash output of which python. That is the usual way to copy a conda or pip venv into a Pycharm or Idea environment.

                      – J_H
                      Nov 22 '18 at 17:38
















                    1












                    1








                    1







                    The env var PYTHONPATH is relevant. In both situations, print the value of sys.path:



                    import sys
                    print('n'.join(sys.path))


                    In bash, you may find it helpful to enter the venv and then $ open /Applications/PyCharm.app (or whatever it's called on your Mac). Then the running pycharm will inherit PYTHONPATH from bash.



                    Here is another approach. Within pycharm type CMD-, (command comma) for preferences, search for Project Structure, right click on the edamame/project folder, and choose alt-S Sources. This will add the folder to your path within pycharm. Again, use print() to verify sys.path contains what you expect it to contain.






                    share|improve this answer













                    The env var PYTHONPATH is relevant. In both situations, print the value of sys.path:



                    import sys
                    print('n'.join(sys.path))


                    In bash, you may find it helpful to enter the venv and then $ open /Applications/PyCharm.app (or whatever it's called on your Mac). Then the running pycharm will inherit PYTHONPATH from bash.



                    Here is another approach. Within pycharm type CMD-, (command comma) for preferences, search for Project Structure, right click on the edamame/project folder, and choose alt-S Sources. This will add the folder to your path within pycharm. Again, use print() to verify sys.path contains what you expect it to contain.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 1:20









                    J_HJ_H

                    4,4441922




                    4,4441922













                    • I print the sys.path for both cases. It is long, so I added to the original questions.

                      – Edamame
                      Nov 22 '18 at 5:53











                    • I hadn't realized you were following the developers.google.com/optimization/install/python/mac instructions. To adapt them to pycharm, try clicking CMD-comma and Add an Existing environment Project Interpreter which matches the bash output of which python. That is the usual way to copy a conda or pip venv into a Pycharm or Idea environment.

                      – J_H
                      Nov 22 '18 at 17:38





















                    • I print the sys.path for both cases. It is long, so I added to the original questions.

                      – Edamame
                      Nov 22 '18 at 5:53











                    • I hadn't realized you were following the developers.google.com/optimization/install/python/mac instructions. To adapt them to pycharm, try clicking CMD-comma and Add an Existing environment Project Interpreter which matches the bash output of which python. That is the usual way to copy a conda or pip venv into a Pycharm or Idea environment.

                      – J_H
                      Nov 22 '18 at 17:38



















                    I print the sys.path for both cases. It is long, so I added to the original questions.

                    – Edamame
                    Nov 22 '18 at 5:53





                    I print the sys.path for both cases. It is long, so I added to the original questions.

                    – Edamame
                    Nov 22 '18 at 5:53













                    I hadn't realized you were following the developers.google.com/optimization/install/python/mac instructions. To adapt them to pycharm, try clicking CMD-comma and Add an Existing environment Project Interpreter which matches the bash output of which python. That is the usual way to copy a conda or pip venv into a Pycharm or Idea environment.

                    – J_H
                    Nov 22 '18 at 17:38







                    I hadn't realized you were following the developers.google.com/optimization/install/python/mac instructions. To adapt them to pycharm, try clicking CMD-comma and Add an Existing environment Project Interpreter which matches the bash output of which python. That is the usual way to copy a conda or pip venv into a Pycharm or Idea environment.

                    – J_H
                    Nov 22 '18 at 17:38




















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421810%2fsame-code-not-working-in-pycharm-but-working-outside-pycharm%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

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)