问题描述
我在 cmd 提示符下运行我的脚本时遇到问题,尽管它在 PyCharm 中工作.我有这样的文件夹结构:
MyCode # PyCharm 项目文件夹/有用的功能/消息/Texter.py/不和谐机器人/DiscordBot.py
在 DiscordBot.py 中我有一个导入
从 UsefulFunctions.Messaging 导入 Texter
当我从 PyCharm 运行它时没有问题.但是,当我尝试从位于 DiscordBot
级别的命令提示符运行时,会出现以下错误:
ImportError: 没有名为UsefulFunctions"的模块
我很自然地认为这意味着 UsefulFunctions
文件夹不在我的路径上.因此,我进入了我的环境变量并将其添加到我的 PATH
变量(以及 MyCode
文件夹中,以防万一).它仍然遇到这个错误.我在这里浏览了一些关于导入的帖子(主要是
如果你想从 cmdline 中运行,你也必须在那里做同样的事情:
[cfati@CFATI-5510-0:e:WorkDevStackOverflowq054955891DiscordBot]>sopr.bat*** 当粘贴到 StackOverflow(或其他)页面时,设置更短的提示以更好地适应 ***[提示]>设置py环境变量 py 未定义[提示]>e:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe"DiscordBot.py回溯(最近一次通话最后):文件DiscordBot.py",第 1 行,在 <module>从 UsefulFunctions.Messaging 导入 TexterModuleNotFoundError:没有名为UsefulFunctions"的模块[提示]>设置 PYTHONPATH=e:WorkDevStackOverflowq054955891[提示]>设置pyPYTHONPATH=e:WorkDevStackOverflowq054955891[提示]>e:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe"DiscordBot.pye:WorkDevStackOverflowq054955891UsefulFunctionsMessagingTexter.py 已导入
作为旁注,我个人讨厌以 My 开头的名称(例如 MyCode).尝试找到一个更有用的名称(例如 TestBotProject 或 smth 类似的名称):).
I am having a problem running my script in a cmd prompt despite it working in PyCharm. I have a folder structure as such:
MyCode # PyCharm project folder
/UsefulFunctions
/Messaging
/Texter.py
/DiscordBot
/DiscordBot.py
Within DiscordBot.py I have an import
from UsefulFunctions.Messaging import Texter
This works when I run it from PyCharm without a problem. However when I try to run from a command prompt located at the DiscordBot
level it errors with:
So naturally I thought it meant that the UsefulFunctions
folder was not on my path. Therefore, I went into my environment variables and added it to my PATH
variable (as well as the MyCode
folder for good measure). Still it encountered this error. I browsed some posts on here regarding imports (mainly Importing files from different folder) and they recommend doing something like:
import sys
sys.path.insert(0, '/path/to/application/app/folder')
import file
Or adding __init__.py
files to each folder in order to get them to register as packages. I went ahead and added __init__
files to each folder and subfolder I was trying to import from, but still could not run from the command prompt...I ommitted the sys.path.insert()
solution because I see no benefit from this after already explicitly adding it to my PATH
variable. Another solution was to add "." before the import because supposedly otherwise it is only searching python's PATH. I attempted this as:
from .UsefulFunctions.Messaging import Texter
And this error shows on PyCharm now as well... I don't get why my initial script would work without a hitch on PyCharm, but the same program cannot seem to find my import when run from a prompt. Can somebody please explain the difference between PyCharm running the program and my prompt? Why will this not work despite having __init__.py
files and having added MyCode
and UsefulFunctions
to my PATH variable on Windows?
From [Python 3.Docs]: Command line and environment - PYTHONPATH:
You can also find more details on [SO]: Strange error while using Pycharm to debug PyQt gui (@CristiFati's answer).
So, in order for Python to be able to load a module (package) without specifying its path, the path must be present in %PYTHONPATH% environment variable.
You mentioned %PATH% several times in the question but it's %PYTHONPATH% (MyCode must be added to it).
PyCharm does that because of (any of) the 2 checkboxes in the image below:
If you want to get things working from cmdline, yo have to do the same thing there as well:
As a side note, I personally hate names that start with My (e.g. MyCode). Try finding a more useful name (e.g. TestBotProject, or smth similar) :).
这篇关于PyCharm 如何导入不同于系统命令提示符 (Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!