[英]Python works in PyCharm but not from terminal
I recently figured out how to import modules for unittesting in python.我最近想出了如何在 python 中导入模块进行单元测试。 As a solution to this, I use:作为解决方案,我使用:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from Dev.test import someclass
This works fine while running in PyCharm and I get the expected output.这在 PyCharm 中运行时工作正常,我得到了预期的输出。 However, when I run from terminal I run into an error:但是,当我从终端运行时,我遇到了一个错误:
ImportError: No module named Dev.test
I have the init files where they are supposed to be but I'm lost as to why this is working in PyCharm but not from the terminal.我有它们应该在的地方的init文件,但我不知道为什么这在 PyCharm 中工作而不是在终端中工作。 I have not changed my path or anything in PyCharm as this code is supposed to be able to run with minimal modifications on other machines.我没有更改我的路径或 PyCharm 中的任何内容,因为此代码应该能够在其他机器上以最少的修改运行。 Any idea as to why this is happening and what I might be able to do to fix it?关于为什么会发生这种情况以及我可以做些什么来解决它的任何想法?
My folder structure is as follows我的文件夹结构如下
-Current
-Dev
-__init__.py
-test
- __init__.py
-someclass.py
-Tests
-__init__.py
-someunittest.py
I have tried running someunittest from the main folder as well as with a complete path but it only works in PyCharm我曾尝试从主文件夹以及完整路径运行 someunittest,但它仅适用于 PyCharm
sys.path.append(os.getcwd()[:os.getcwd().index('Dev')])
I added this to my imports and it seems to have solved the problem.我将此添加到我的导入中,它似乎解决了问题。 However, this doesn't seem like it would be the right way to do it;然而,这似乎不是正确的做法。 it will do for now.现在就可以了。
When running a script from within PyCharm, it runs it in an environment with PYTHONPATH
set to the list of all the folders that are marked "Sources Root" (with a blue folder icon) in the project explorer.从 PyCharm 中运行脚本时,它会在PYTHONPATH
设置为项目资源管理器中标记为“Sources Root”(带有蓝色文件夹图标)的所有文件夹列表的环境中运行它。
Outside of PyCharm, PYTHONPATH
is not normally set.在 PyCharm 之外,通常不会设置PYTHONPATH
。 The first entry in sys.path
refers to the current working directory where the script was run from. sys.path
的第一项是指运行脚本的当前工作目录。 As long as you run your script with your terminal's working directory as the folder containing Dev
, it should be able to find the Dev.test
module, regardless of the extra entry added to sys.path
.只要您使用终端的工作目录作为包含Dev
的文件夹运行脚本,它就应该能够找到Dev.test
模块,而不管添加到sys.path
的额外条目如何。
Once you get the working directory correct, you should be able to remove the sys.path
hack.一旦你得到正确的工作目录,你应该能够删除sys.path
hack。
I too have had this issue - and the PYTHONPATH setting set by PyCharm did seem to be the issue.我也有这个问题 - PyCharm 设置的 PYTHONPATH 设置似乎确实是问题所在。
My alternative (as I was nearly finished writing the code) was to generate a setup.py - and install the classes/structure in my local virtual python environment.我的替代方案(因为我几乎完成了代码的编写)是生成一个 setup.py - 并在我的本地虚拟 python 环境中安装类/结构。
Hope this helps..希望这可以帮助..
What @codewarrior has said about the PyCharm setting its own PYTHONPATH
is correct. @codewarrior 关于 PyCharm 设置自己的PYTHONPATH
说法是正确的。 But sys.path
didn't have my current working directory.但是sys.path
没有我当前的工作目录。 So to get around this problem, I updated my PYTHONPATH
(or you can edit sys.path
).所以为了解决这个问题,我更新了我的PYTHONPATH
(或者你可以编辑sys.path
)。
Setting PYTHONPATH
设置PYTHONPATH
export PYTHONPATH=$PYTHONPATH:`pwd` (OR your project root directory)
Updating sys.path
更新sys.path
import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')
You can use insert/append based on the order in which you want your project to be searched.您可以根据希望搜索项目的顺序使用插入/追加。
HTH.哈。
Pycharm uses a virtual environment. Pycharm 使用虚拟环境。 When you try run your program in your terminal this enviroment isn't active.当您尝试在终端中运行程序时,此环境未处于活动状态。
You need to build or upload your enviroment Pycharm with your libraries.您需要使用您的库构建或上传您的环境 Pycharm。 cd to the directory project and write in terminal : cd 到目录项目并在终端中写入:
source venv/bin/activate
I would recommend trying out $ pip install .
我建议尝试$ pip install .
in your source directory.在您的源目录中。 This will install your own packages for your project.这将为您的项目安装您自己的软件包。
To add to similar answers here, PyCharm is doing some extra config for you before running your script.在这里添加类似的答案,PyCharm 会在运行脚本之前为您做一些额外的配置。 If adding your sources root to PYTHONPATH doesn't work then examine your run configuration in PyCharm for the script in question, there will likely be some more behind the scenes magic at play.如果将您的源根目录添加到 PYTHONPATH 不起作用,则在 PyCharm 中检查您在 PyCharm 中的运行配置是否有问题,可能会有更多幕后魔法在起作用。
I had similar problem.我有类似的问题。 I think the problem is that Pycharm modifies PYTHONPATH so before running your script:我认为问题在于 Pycharm 在运行脚本之前修改了 PYTHONPATH:
You can also create "main" python file where you set the python path and then call the other modules您还可以创建“主”python 文件,您可以在其中设置 python 路径,然后调用其他模块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.