Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

python - How PyCharm imports differently than system command prompt (Windows)

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:

ImportError: No module named 'UsefulFunctions'

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

ImportError: attempted relative import with no known parent package

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From [Python 3.Docs]: Command line and environment - PYTHONPATH:

Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.

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:

Img0

If you want to get things working from cmdline, yo have to do the same thing there as well:

[cfati@CFATI-5510-0:e:WorkDevStackOverflowq054955891DiscordBot]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> set py
Environment variable py not defined

[prompt]> "e:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe" DiscordBot.py
Traceback (most recent call last):
  File "DiscordBot.py", line 1, in <module>
    from UsefulFunctions.Messaging import Texter
ModuleNotFoundError: No module named 'UsefulFunctions'

[prompt]> set PYTHONPATH=e:WorkDevStackOverflowq054955891

[prompt]> set py
PYTHONPATH=e:WorkDevStackOverflowq054955891

[prompt]> "e:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe" DiscordBot.py
e:WorkDevStackOverflowq054955891UsefulFunctionsMessagingTexter.py imported

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) :).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...