2013-07-05 56 views
6

我正在開發我的第一個主要的python應用程序,並且在環境方面遇到了一些麻煩。我正在嘗試使用python控制檯(在pycharm中)。當我火了控制檯,我得到這個 -pycharm的python控制檯中的路徑一級太低

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文件夾和那些未能找到。

我應該什麼IM要求的是什麼命令來告訴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 

* *目錄結構

gitflow_plus (project root folder) 
    bin 
    gitflow 
    bin 
    conf 
     __init__.py 
     configmanager.py 
     class ConfigManager 
    __init__.py 
    exceptions.py 
     class exception1 
     class exception2 
     class exception3 
    tests 
    fixtures 
    gitflow 
    helpers 
    usecase 

==================更多努力

我一直在讀了,我發現這個東西:

>>> __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 

======= ========= 然而更多信息

>>> 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'] 

======================= 哎,小breakthru。我退後一步,認爲'conf'這個詞可能是一個保留字或一個已經存在的軟件包。所以我將它重命名爲config。現在,我得到

>> import config.configmanager 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/home/scphantm/code/gitflow_plus/gitflow/config/configmanager.py", line 7, in <module> 
    from exceptions import NoRepositoryObject 
ImportError: cannot import name NoRepositoryObject 

現在,從您在上面看到的目錄結構中,您會看到例外情況出現在項目的根目錄中。爲什麼現在能夠找到模塊和類,但模塊代碼無法在根中找到某些東西?

================================ 你們可以拉我的項目,如果你想給一個裂縫在它。其在

[email protected]:scphantm/gitflow_plus.git 

即時通訊仍然難倒。

=============== 我還在學習命令。當我這樣做

>>>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'>} 

大長的事情,但有趣的是,這條線

'exceptions': <module 'exceptions' (built-in)> 

有趣的是,它包含我的所有的異常也被稱爲異常文件。只要我將該文件重命名爲flow_exceptions,一切都亮起來,工作得很好。那麼這僅僅用了一半的天= - )

+0

[這是一個鏈接](http://docs.python.org/3.3/reference/import.html)用於導入使用Python 3.3,和[這裏是相同的鏈接](http://docs.python .org/2.7/tutorial/modules.html)。 你能顯示相關的目錄結構嗎? –

+0

那裏你去。如果您需要其他信息,請告訴我。我只是在這裏旋轉我的輪子 – scphantm

+0

什麼不適合你_does_爲我工作。你使用Python 2.7嗎? –

回答

0

如果你真的只是想做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。

+0

OSError:[Errno 2]沒有這樣的文件或目錄:'gitflow'呃,這不應該是這麼難。你知道如何使用這個東西說一頁教程? – scphantm

+0

[這裏是](http://www.tutorialspoint.com/python/os_chdir.htm)os.chdir上的一個小教程,[here](http://docs.python.org/2/library/os .html)是「os」模塊文檔的鏈接。 –

+0

我剛剛做了一個getcwd(見上面的編輯)看起來不是那樣的我的問題,因爲我已經在gitflow文件夾中。還有什麼可以做到這一點?我的gitflow文件夾被設置爲一個源文件夾,我會想象它將它放在python版本的classpath中。 – scphantm

1
  1. 打開項目設置,然後在「項目結構」 FILE->SETTINGS->PROJECT STRUCTURE

  2. 瀏覽到要作爲基本路徑,並指定爲「源」, 進口和自動提示將啓動目錄按照您的要求工作。

+1

昨天試過。 gitflow和測試文件夾都被標記爲源文件夾。 – scphantm

0

您是否嘗試將您的應用程序目錄標記爲源根目錄?右鍵單擊PyCharm中的應用程序目錄,然後懸停在Mark Directory As菜單上,然後選擇Source Root。這可能有幫助。

3

經過多次調試,閱讀和學習後,我發現我的問題非常簡單。命令

sys.modules 

列出了由python加載的所有模塊。當讀取輸出時,我意識到conf是一個被加載的另一個模塊中列出的模塊,並且模塊異常由基本python使用。當我將conf重命名爲config和flow_exceptions的異常時,所有工作都完美無缺。

謝謝大家。我要離開上面的unmyleted,希望我的思維過程在調試這可以幫助別人。

+1

如果問題解決了,您應該接受您的答案。 –

+0

我不得不再等幾個小時才能讓我 – scphantm

+1

這對我也有幫助(我的模塊創造性地命名爲'code')。 G'wan - 對待自己接受的答案。你已經爲他人提供了一個適中的26,386小時鐘聲。 – kungfujam

0

什麼似乎對我的工作是

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

出於某種原因,似乎沒有如果目錄被標記爲「SRC」,或者無關緊要的,包括在Python控制檯中的SRC文件的相關設置被檢查。