Script running in PyCharm but not from the command line

我怕爱的太早我们不能终老 提交于 2019-12-28 04:01:14

问题


When I try to run my program from the PyCharm IDE everything works fine but if I type in Fedora:

python myScript.py

in a shell prompt I get an import error from 1 of the module.

ImportError : No modue named myDependency

What does PyCharm do that allows the interpreter to find my dependencies when launched from the IDE? How can I get my script to find its dependencies so it can be launched with a singe command?


回答1:


There are a few possible things that can be causing this:

  1. The same python interpreter? Check with import sys; print(sys.executable)
  2. Is it the same working directory? Check with import os; print(os.getcwd())
  3. Discrepancies in sys.path, which is the list python searches sequentially for import locations, can possibly caused by environment variables. Check with import sys; print(sys.path).



回答2:


Adding this worked for me:

from os import sys, path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))



回答3:


As a first step, in PyCharm go to project settings / python interpreter, and note the path. Then compare that to the result of which python -- do they line up? If not, you need to make them do so.

If that's ok, check what PyCharm defines as your project root in project settings / project structure. Is that the directory where your script is located? If not, you should run the script from that directory or append the directory to the $PYTHONPATH variable.

Almost definitely, it's one of those two things.




回答4:


You might have set some project dependency in Pycharm for module myDependency. You can access the same in Fedora by importing the module explicitly or by creating the egg of that module and installing it. This will then go to python site-packages from where you can refer this dependency.



来源:https://stackoverflow.com/questions/29553668/script-running-in-pycharm-but-not-from-the-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!