最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】PyCharm中调试pipenv虚拟环境中文件出错:FileNotFoundError Errno 2 No such file or directory pipenv run python py

PyCharm crifan 11574浏览 0评论

折腾:

【已解决】PyCharm中如何调试pipenv的虚拟环境中的python3的文件

期间,得知Mac本地shell中,可以通过:

<code>pipenv run python testRestApi.py
</code>

去是可以正常运行pipenv的虚拟环境中的python3的代码的。

所以此处再去把这个配置到PyCharm中,希望可以实现在PyCharm中正常调试pipenv虚拟环境中的python3的文件。

结果用配置:

去运行出错:

<code>/Users/crifan/.virtualenvs/robotDemo-HXjMJQEQ/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 58605 --file "pipenv run python testRestApi.py"
pydev debugger: process 80603 is connecting

Connected to pydev debugger (build 173.4127.16)
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1668, in &lt;module&gt;
    main()
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 11, in execfile
    stream = tokenize.open(file)  # @UndefinedVariable
  File "/Users/crifan/.virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/tokenize.py", line 452, in open
    buffer = _builtin_open(filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'pipenv run python testRestApi.py'
</code>

找了找配置,感觉是自己忘了配置工作目录working directory:

所以去加上working directory为当前pipenv的项目的根目录:

Working directory: /Users/crifan/dev/dev_root/company/naturling/projects/robotDemo

结果问题依旧:

还是找不到文件。

看到代码前面有提示:

所以点击看看

是可以进入调试的:

也是能正常输出的:

然后去测试api也是OK的:

点击测试,执行到断点了:

然后可以正常输出的:

对比系统生成的debug:

和自己添加的debug:

主要就是script path不一样

其是:/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/testRestApi.py

自己是:pipenv run python testRestApi.py

但是自己的,在加了working directory后,也不应该错啊

把自己的解析器换成 project default的那个(虽然感觉没啥区别)

参考:

【已解决】Mac中PyCharm中去加断点实时调试scrapy的项目

中的:

才想起来:

原来PyCharm的debug的配置中的script path,就是要输入对应的python脚本文件

然后在Parameters中,输入必要的参数

-》而此处,感觉应该是:

输入pipenv这个,如果是python的话的,的脚本的路径,最好是绝对路径

然后加上参数,或许是:run python testRestApi.py

去找找,是否有pipenv这个python文件

<code>➜  robotDemo which pipenv
/Users/crifan/Library/Python/3.6/bin/pipenv
➜  robotDemo ls -lha /Users/crifan/Library/Python/3.6/
total 0
drwx------  4 crifan  staff   128B  4 17 11:06 .
drwx------  4 crifan  staff   128B  4 17 11:06 ..
drwxr-xr-x  7 crifan  staff   224B  4 17 14:35 bin
drwx------  3 crifan  staff    96B  4 17 11:06 lib
➜  robotDemo ls -lha /Users/crifan/Library/Python/3.6/bin/
total 40
drwxr-xr-x  7 crifan  staff   224B  4 17 14:35 .
drwx------  4 crifan  staff   128B  4 17 11:06 ..
-rwxr-xr-x  1 crifan  staff   242B  4 17 14:35 pewtwo
-rwxr-xr-x  1 crifan  staff   226B  4 17 14:35 pipenv
-rwxr-xr-x  1 crifan  staff   237B  4 17 14:35 pipenv-resolver
-rwxr-xr-x  1 crifan  staff   232B  4 17 11:06 virtualenv
-rwxr-xr-x  1 crifan  staff   237B  4 17 11:06 virtualenv-clone
➜  robotDemo cat /Users/crifan/Library/Python/3.6/bin/pipenv
#!/usr/local/opt/python/bin/python3.6

# -*- coding: utf-8 -*-
import re
import sys

from pipenv import cli

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(cli())
</code>

好像这个pipenv还真是一个python文件呢。

而另外,感觉是:

此处由于PyCharm已经使用了此处的虚拟环境中的python解析器了,已同时识别了相关的库,所以貌似也不需要再去加上

<code>pipenv run python
</code>

去运行这个处于(pipenv创建的)虚拟环境中的py文件了。

所以,上述(点击运行按钮,选择Debug xxx)用PyCharm自带的debug,就可以正常运行此处的python文件了

-》否则会报错,找不到Flask才对的(本身Mac的默认的python2.7的环境中并没有安装Flask)

-》而这个默认的debug配置也已经足够用了。

所以去试试pipenv作为文件,然后加上参数:

结果是:

找不到pipenv:

FileNotFoundError: [Errno 2] No such file or directory: ‘pipenv’

加上pipenv的完整路径后:

结果:

<code>/Users/crifan/.virtualenvs/robotDemo-HXjMJQEQ/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 62489 --file /Users/crifan/Library/Python/3.6/bin/pipenv run python testRestApi.py
pydev debugger: process 80863 is connecting

Connected to pydev debugger (build 173.4127.16)
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1668, in &lt;module&gt;
    main()
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/crifan/Library/Python/3.6/bin/pipenv", line 7, in &lt;module&gt;
    from pipenv import cli
ModuleNotFoundError: No module named 'pipenv'

</code>

pycharm from pipenv import cli ModuleNotFoundError No module named pipenv

ModuleNotFoundError when running installed packages · Issue #98 · pypa/pipenv

ModuleNotFoundError when running installed packages · Issue #98 · pypa/pipenv

python – No module named … in PyCharm (import error) – Stack Overflow

此处解析器是没有问题的:

pycharm debug  ModuleNotFoundError No module named pipenv

PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy’ – CSDN博客

pycharm 提示 No module named ‘XXX’ – CSDN博客

pycharm    ModuleNotFoundError No module named pipenv

pycharm报错:ModuleNotFoundError: No module named ‘XXX’ – CSDN博客

没有解决此处问题,顺带看看:

以及:

好像设置中的

有点关系,或者是一样?

有空再去操心

而此处之所以找不到pipenv的模块,估计就是别人说的:

此处已经在pipenv创建的虚拟环境中了,而此虚拟环境中所安装的模块,现在只有:

<code>➜  robotDemo pipenv graph
Flask==0.12.2
  - click [required: &gt;=2.0, installed: 6.7]
  - itsdangerous [required: &gt;=0.21, installed: 0.24]
  - Jinja2 [required: &gt;=2.4, installed: 2.10]
    - MarkupSafe [required: &gt;=0.23, installed: 1.0]
  - Werkzeug [required: &gt;=0.7, installed: 0.14.1]
</code>

而且也可以从PyCharm中看到:

此处是没有,所谓的pipenv的模块的

-》导致pycharm的cli命令行去导入pipenv,无法导入。

再加上,之前提到的,pycharm自带创建的调试配置,其中的脚本就是此处的py文件

而无需pipenv的命令去运行,因为本身已经在虚拟环境中了。

所以可以正常运行,正常调试。

【总结】

最终是:

放弃这种自己加pipenv去运行的方式。因为本身不可行:

对于pycharm来说,在这个pipenv创建的虚拟环境中,本身不存在所谓的pipenv的模块

采用:

直接设置py文件的方式:

可以正常调试:

转载请注明:在路上 » 【已解决】PyCharm中调试pipenv虚拟环境中文件出错:FileNotFoundError Errno 2 No such file or directory pipenv run python py

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.162 seconds, using 22.18MB memory