pycharm的python控制台中的路径级别过低

pycharm的python控制台中的路径级别过低,python,pycharm,Python,Pycharm,我正在开发我的第一个主要python应用程序,并且在环境方面遇到了一些问题。我正在尝试使用python控制台(在pycharm中)。当我打开控制台时,我得到了这个- sys.path.extend(['/home/scphantm/code/gitflow_plus', '/home/scphantm/code/gitflow_plus/gitflow', '/home/scphantm/code/gitflow_plus/bin', '/home/scphantm/code/gitflow_p

我正在开发我的第一个主要python应用程序,并且在环境方面遇到了一些问题。我正在尝试使用python控制台(在pycharm中)。当我打开控制台时,我得到了这个-

sys.path.extend(['/home/scphantm/code/gitflow_plus', '/home/scphantm/code/gitflow_plus/gitflow', '/home/scphantm/code/gitflow_plus/bin', '/home/scphantm/code/gitflow_plus/tests'])
当我这样做的时候-

>>> sys.path
    ['/usr/local/pycharm/helpers/pydev', '/usr/local/lib/python2.7/dist-packages/GitPython-0.3.2.RC1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/gitdb-0.5.4-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/smmap-0.8.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/async-0.6.1-py2.7-linux-x86_64.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/home/scphantm/code/gitflow_plus', '/home/scphantm/code/gitflow_plus/gitflow', '/home/scphantm/code/gitflow_plus/bin', '/home/scphantm/code/gitflow_plus/tests']
现在,如果我尝试导入我的一个项目文件,我必须这样做,例如

import gitflow.conf.configmanager
而不是简单地

import conf.configmanager
问题是,配置管理器具有与gitflow文件夹相关的导入,但找不到这些导入

我想我要问的是命令是什么来告诉python控制台有效地运行

cd gitflow
**更多信息

>>> os.getcwd()
'/home/scphantm/Code/gitflow_plus/gitflow'
>>> from conf.configmanager import ConfigManager
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named configmanager
>>> import conf.configmanager
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named configmanager
>>> dir(conf)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'conf' is not defined
>>> dir(gitflow)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'gitflow' is not defined
>>> import gitflow.conf.configmanager
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/scphantm/code/gitflow_plus/gitflow/conf/configmanager.py", line 7, in <module>
    from exceptions import NoRepositoryObject
ImportError: cannot import name NoRepositoryObject
>>> dir(sys)
['__displayhook__', '__doc__', '__egginsert', '__excepthook__', '__name__', '__package__', '__plen', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', '_multiarch', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'last_traceback', 'last_type', 'last_value', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']
=================================更多尝试

我一直在阅读,我发现了这些东西:

>>> __name__
'__main__'
>>> __package__
>>> __package__ = 'gitflow'
>>> from conf.configmanager import ConfigManager
/usr/local/pycharm/helpers/pydev/pydevconsole.py:1: RuntimeWarning: Parent module 'gitflow' not found while handling absolute import
  try:
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named conf.configmanager
我仍然感到困惑

=============== 我还在学习命令。当我这么做的时候

>>>sys.modules
{'heapq': <module 'heapq' from '/usr/lib/python2.7/heapq.pyc'>, 'code': <module 'code' from '/usr/lib/python2.7/code.pyc'>, 'distutils': <module 'distutils' from '/usr/lib/python2.7/distutils/__init__.pyc'>, 'functools': <module 'functools' from '/usr/lib/python2.7/functools.pyc'>, 'random': <module 'random' from '/usr/lib/python2.7/random.pyc'>, 'datetime': <module 'datetime' from '/usr/lib/python2.7/lib-dynload/datetime.x86_64-linux-gnu.so'>, 'sysconfig': <module 'sysconfig' from '/usr/lib/python2.7/sysconfig.pyc'>, 'encodings.encodings': None, 'xml': <module 'xml' from '/usr/lib/python2.7/xml/__init__.pyc'>, 'struct': <module 'struct' from '/usr/lib/python2.7/struct.pyc'>, 'tempfile': <module 'tempfile' from '/usr/lib/python2.7/tempfile.pyc'>, 'base64': <module 'base64' from '/usr/lib/python2.7/base64.pyc'>, 'pyexpat.errors': <module 'pyexpat.errors' (built-in)>, 'collections': <module 'collections' from '/usr/lib/python2.7/collections.pyc'>, 'distutils.types': None, 'zipimport': <module 'zipimport' (built-in)>, 'string': <module 'string' from '/usr/lib/python2.7/string.pyc'>, 'SocketServer': <module 'SocketServer' from '/usr/lib/python2.7/SocketServer.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from '/usr/lib/python2.7/encodings/utf_8.pyc'>, 'ssl': <module 'ssl' from '/usr/lib/python2.7/ssl.pyc'>, 'distutils.re': None, 'httplib': <module 'httplib' from '/usr/lib/python2.7/httplib.pyc'>, 'config.exceptions': None, 'signal': <module 'signal' (built-in)>, 'threading': <module 'threading' from '/usr/lib/python2.7/threading.pyc'>, 'pyexpat.model': <module 'pyexpat.model' (built-in)>, 'distutils.version': <module 'distutils.version' from '/usr/lib/python2.7/distutils/version.pyc'>, 'cStringIO': <module 'cStringIO' (built-in)>, 'pygit2.repository': <module 'pygit2.repository' from '/usr/local/lib/python2.7/dist-packages/pygit2/repository.pyc'>, 'xml.parsers.expat': <module 'xml.parsers.expat' from '/usr/lib/python2.7/xml/parsers/expat.pyc'>, 'encodings': <module 'encodings' from '/usr/lib/python2.7/encodings/__init__.pyc'>, 'BaseHTTPServer': <module 'BaseHTTPServer' from '/usr/lib/python2.7/BaseHTTPServer.py'>, 'pydev_imports': <module 'pydev_imports' from '/usr/local/pycharm/helpers/pydev/pydev_imports.pyc'>, 'config.distutils': None, 'abc': <module 'abc' from '/usr/lib/python2.7/abc.pyc'>, 'rfc822': <module 'rfc822' from '/usr/lib/python2.7/rfc822.pyc'>, 'urllib': <module 'urllib' from '/usr/lib/python2.7/urllib.pyc'>, 're': <module 're' from '/usr/lib/python2.7/re.pyc'>, 'math': <module 'math' (built-in)>, 'fcntl': <module 'fcntl' (built-in)>, 'UserDict': <module 'UserDict' from '/usr/lib/python2.7/UserDict.pyc'>, 'Queue': <module 'Queue' from '/usr/lib/python2.7/Queue.pyc'>, 'codecs': <module 'codecs' from '/usr/lib/python2.7/codecs.pyc'>, '_sysconfigdata_nd': <module '_sysconfigdata_nd' from '/usr/lib/python2.7/plat-x86_64-linux-gnu/_sysconfigdata_nd.pyc'>, '_functools': <module '_functools' (built-in)>, 'socket': <module 'socket' from '/usr/lib/python2.7/socket.pyc'>, 'thread': <module 'thread' (built-in)>, 'StringIO': <module 'StringIO' from '/usr/lib/python2.7/StringIO.pyc'>, 'traceback': <module 'traceback' from '/usr/lib/python2.7/traceback.pyc'>, 'itertools': <module 'itertools' (built-in)>, 'config.os': None, 'os': <module 'os' from '/usr/lib/python2.7/os.pyc'>, '__future__': <module '__future__' from '/usr/lib/python2.7/__future__.pyc'>, '_collections': <module '_collections' (built-in)>, '_sre': <module '_sre' (built-in)>, '__builtin__': <module '__builtin__' (built-in)>, 'xml.parsers': <module 'xml.parsers' from '/usr/lib/python2.7/xml/parsers/__init__.pyc'>, 'operator': <module 'operator' (built-in)>, 'xml.parsers.pyexpat': None, 'array': <module 'array' (built-in)>, 'distutils.string': None, 'select': <module 'select' (built-in)>, '_heapq': <module '_heapq' (built-in)>, 'posixpath': <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>, 'errno': <module 'errno' (built-in)>, '_socket': <module '_socket' (built-in)>, 'binascii': <module 'binascii' (built-in)>, 'sre_constants': <module 'sre_constants' from '/usr/lib/python2.7/sre_constants.pyc'>, 'os.path': <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>, 'config.pygit2': None, '_warnings': <module '_warnings' (built-in)>, 'pygit2._pygit2': None, 'encodings.__builtin__': None, '_codecs': <module '_codecs' (built-in)>, 'SimpleXMLRPCServer': <module 'SimpleXMLRPCServer' from '/usr/lib/python2.7/SimpleXMLRPCServer.py'>, 'pydev_console_utils': <module 'pydev_console_utils' from '/usr/local/pycharm/helpers/pydev/pydev_console_utils.pyc'>, '_sysconfigdata': <module '_sysconfigdata' from '/usr/lib/python2.7/_sysconfigdata.pyc'>, '_struct': <module '_struct' (built-in)>, 'hashlib': <module 'hashlib' from '/usr/lib/python2.7/hashlib.pyc'>, 'keyword': <module 'keyword' from '/usr/lib/python2.7/keyword.pyc'>, 'zlib': <module 'zlib' (built-in)>, 'posix': <module 'posix' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from '/usr/lib/python2.7/encodings/aliases.pyc'>, 'exceptions': <module 'exceptions' (built-in)>, 'sre_parse': <module 'sre_parse' from '/usr/lib/python2.7/sre_parse.pyc'>, 'mimetools': <module 'mimetools' from '/usr/lib/python2.7/mimetools.pyc'>, 'copy_reg': <module 'copy_reg' from '/usr/lib/python2.7/copy_reg.pyc'>, 'sre_compile': <module 'sre_compile' from '/usr/lib/python2.7/sre_compile.pyc'>, '_hashlib': <module '_hashlib' from '/usr/lib/python2.7/lib-dynload/_hashlib.x86_64-linux-gnu.so'>, '_random': <module '_random' (built-in)>, 'site': <module 'site' from '/usr/lib/python2.7/site.pyc'>, 'io': <module 'io' from '/usr/lib/python2.7/io.pyc'>, '__main__': <module '__main__' from '/usr/local/pycharm/helpers/pydev/pydevconsole.py'>, 'pyexpat': <module 'pyexpat' from '/usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so'>, 'urlparse': <module 'urlparse' from '/usr/lib/python2.7/urlparse.pyc'>, 'strop': <module 'strop' (built-in)>, 'linecache': <module 'linecache' from '/usr/lib/python2.7/linecache.pyc'>, 'encodings.codecs': None, '_abcoll': <module '_abcoll' from '/usr/lib/python2.7/_abcoll.pyc'>, 'config': <module 'config' from '/home/scphantm/code/gitflow_plus/gitflow/config/__init__.pyc'>, 'pygit2': <module 'pygit2' from '/usr/local/lib/python2.7/dist-packages/pygit2/__init__.pyc'>, 'genericpath': <module 'genericpath' from '/usr/lib/python2.7/genericpath.pyc'>, 'stat': <module 'stat' from '/usr/lib/python2.7/stat.pyc'>, '_ssl': <module '_ssl' from '/usr/lib/python2.7/lib-dynload/_ssl.x86_64-linux-gnu.so'>, 'warnings': <module 'warnings' from '/usr/lib/python2.7/warnings.pyc'>, 'pydevd_constants': <module 'pydevd_constants' from '/usr/local/pycharm/helpers/pydev/pydevd_constants.pyc'>, 'encodings.ascii': <module 'encodings.ascii' from '/usr/lib/python2.7/encodings/ascii.pyc'>, 'pydev_localhost': <module 'pydev_localhost' from '/usr/local/pycharm/helpers/pydev/pydev_localhost.pyc'>, 'textwrap': <module 'textwrap' from '/usr/lib/python2.7/textwrap.pyc'>, 'sys': <module 'sys' (built-in)>, 'codeop': <module 'codeop' from '/usr/lib/python2.7/codeop.pyc'>, 'pygit2.string': None, 'types': <module 'types' from '/usr/lib/python2.7/types.pyc'>, '_pygit2': <module '_pygit2' from '/usr/local/lib/python2.7/dist-packages/_pygit2.so'>, 'sitecustomize': <module 'sitecustomize' from '/usr/lib/python2.7/sitecustomize.pyc'>, '_weakref': <module '_weakref' (built-in)>, 'pygit2.version': <module 'pygit2.version' from '/usr/local/lib/python2.7/dist-packages/pygit2/version.pyc'>, 'xmlrpclib': <module 'xmlrpclib' from '/usr/lib/python2.7/xmlrpclib.pyc'>, '_io': <module '_io' (built-in)>, '_weakrefset': <module '_weakrefset' from '/usr/lib/python2.7/_weakrefset.pyc'>, 'time': <module 'time' (built-in)>, 'gzip': <module 'gzip' from '/usr/lib/python2.7/gzip.pyc'>}
>>系统模块
{'heapq':,'code':,'distutils':,'functools':,'random':,'datetime':,'sysconfig':,'encodings.encodings':无,'xml':,'struct':,'base64':,'pyexpat.errors':,'collections','distutils.types':无,'zipimport':,'string':,'SocketServer','encodings.utf_8','ssl':,'distutils.re':无,'httplib','config。例外情况:无,'signal':,'threading':,'pyexpat.model':,'distutils.version':,'cStringIO':,'pygit2.repository':,'xml.parsers.expat':,'encodings':,'BaseHTTPServer':,'pydev\u imports','config.distutils':无,'abc','rfc822':,'urllib':,'re','re','math','fcntl','UserDict':,'fcntl','configures','confighttpserver','codes','configurance','codes',',“'u functools':,'socket':,'thread':,'StringIO':,'traceback','itertools':,'config.os':无,'os':,'u future':,'u collections':,'u sre':,'uu内置.'xml.parsers':,'operator','xml.parsers.pyexpat':无,'array','distutils.string':无,'select','sept','heapq','posno:,'socket','sci','sre_常量:,'os.path':,'config.pygit2':无,'pygit2.'pygit2.'pygit2':无,'encodings.'builtin.'UuIu':无,'Uucodecs':,'SimpleXMLRPCServer':,'pydev_控制台_utils','uSysConfigData':,'uStruct','hashlib','keyword','zlib','posix':,'encodings.别名','exceptions','SreXmlrpcserver':,'SreXu:'parse',',‘sre_compile’:,‘u hashlib’:,‘u random’:,‘site’:,‘io’:,‘u main_uuu’:,‘pyexpat’:,‘urlparse’:,‘strop’:‘linecache’:,‘encodings.codecs’:‘None’,‘abcoll’,‘config’,‘pygit2’:‘genericpath’,‘stat’,‘ssl’:‘警告’,‘pydevd’:‘constants’,‘encodings’,‘encodings.ascii’,‘pydev’:‘localhost’,‘pycodesys’,‘gitop’,‘git2’,‘gitop’,‘gitop’,‘git2’,‘gitop’,‘gitop’,‘gito.string':无,'types':,'u pygit2':,'sitecustomize':,'u weakref':,'pygit2.version':,'xmlrpclib':,'u io':,'u weakrefset','time','gzip':}
大而长的东西,但有趣的是这条线

'exceptions': <module 'exceptions' (built-in)>
“例外情况”:
有趣的是,包含我所有异常的文件也被称为异常。当我将该文件重命名为flow_exceptions时,一切都亮了起来,工作正常。好吧,那只花了半天时间=-)

如果你真的只想做
cd-gitflow
,你可以使用
os.chdir('gitflow')
,但我不确定这是不是最优雅的解决方案

import os
os.chdir('path/to/gitflow')
**编辑:不适合你的东西适合我…**

>>> os.getcwd()
'C:\\Development\\gitflow'
>>> from conf.configmanager import ConfigManager
>>> c = ConfigManager()
这里没有错误。我正在Windows上使用Python 3.3

  • 打开项目设置,然后打开“项目结构”
    FILE->settings->project structure

  • 浏览到要用作基本路径的目录,并将其分配给“源”, 导入和自动建议将根据您的要求开始工作


  • 您是否尝试将应用程序目录标记为源根目录?右键单击PyCharm中的应用程序目录,并将鼠标悬停在
    将目录标记为
    菜单上,然后选择
    源根目录
    。这可能会有帮助。

    经过大量的调试、阅读和学习,我发现我的问题很简单。命令

    sys.modules
    
    列出了python加载的所有模块。在读取该输出时,我意识到conf是一个在另一个加载的模块中列出的模块,并且基本python使用了模块异常。当我将conf重命名为config,将exceptions重命名为flow\u exceptions时,一切都运行得很好


    谢谢大家。我将不受上述问题的困扰,希望我在调试过程中的思维过程能够帮助其他人。

    对我来说似乎有效的是

    import sys
    sys.path.extend(['path/to/whatever'])
    

    出于某种原因,目录是否标记为“src”或者是否检查了在Python控制台中包含src文件的相关设置似乎并不重要。

    OSError:[Errno 2]没有这样的文件或目录:“gitflow”啊,这应该不会这么难。你知道关于如何使用这个东西的一页的教程吗?一个关于os.chdir的小教程,是指向
    os
    模块文档的链接。我刚刚做了一个getcwd(见上面的编辑),看起来这不是我的问题,因为我已经在gitflow文件夹中了。还有什么可以这样做?我的gitflow文件夹设置为源文件夹,我想这会把它放在Python版本的classpath中。这很奇怪,因为它只在我使用gitflow.conf.configmanager时才对我有效,这会把事情搞砸,因为configmanager需要从底部的gitflow文件夹导入,除非我将import exceptions更改为import gitflow.exceptions不能这样做,因为这样会在我发布代码时破坏代码。用于使用Python3.3导入,以及用于Python2.7导入。你能展示一下相关的目录结构吗?好了。如果你需要任何其他信息,请告诉我。我只是在这里打转,对你不管用的东西对我管用。你在使用Python2.7吗?嗯,我不知道发生了什么。当我在Python2.7上测试它时,它也工作了。希望一个真正的Pythonista能在这里帮助您。那么gitflow/\uuuuu init\uuuuuuuy.py是否必须有某种对conf的引用才能被看到,或者是隐式完成的?昨天尝试过了。
    >>> os.getcwd()
    'C:\\Development\\gitflow'
    >>> from conf.configmanager import ConfigManager
    >>> c = ConfigManager()
    
    sys.modules
    
    import sys
    sys.path.extend(['path/to/whatever'])