python导入包的正确方法以及如何在pycharm控制台

时间:2015-06-19 15:03:07

标签: python import module pycharm

我目前正在为statsmodels写一个PR分支,这里有完整的代码(https://github.com/statsmodels/statsmodels/pull/2435

这是我的文件夹的结构。

donbeo@donbeo-HP-EliteBook-Folio-9470m:~/Documents/statsmodels/statsmodels/sandbox$ tree gam_gsoc2015/
gam_gsoc2015/
├── draft.py
├── draft.py~
├── gam_example.py
├── gam_example.py~
├── gam_example.R
├── gam.py
├── gam.py~
├── __init__.py
├── __pycache__
│   ├── gam.cpython-34.pyc
│   ├── __init__.cpython-34.pyc
│   └── smooth_basis.cpython-34.pyc
├── smooth_basis.py
└── tests
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-34.pyc
    │   ├── test_gam.cpython-34.pyc
    │   └── test_smooth_basis.cpython-34.pyc
    ├── results
    │   └── prediction_from_mgcv.csv
    ├── test_gam.py
    ├── test_gam.py~
    ├── test_smooth_basis.py
    └── test_smooth_basis.py~

4 directories, 21 files
donbeo@do

来自gam_example.py

test_gam.pygam.py导入函数

我正在使用pycharm社区

我已将gam_gsoc2015标记为sources root,我可以从pycharm运行gam_exampletest_gam文件,但遇到一系列问题:

1)gam_gsoc2015文件夹是所有项目中唯一标有蓝色的文件夹。 (即唯一标记为源根的人)

2)我无法使用nosetest运行测试

donbeo@donbeo-HP-EliteBook-Folio-9470m:~/Documents/statsmodels/statsmodels/sandbox$ nosetests
................EE......................................................S...................
======================================================================
ERROR: Failure: ImportError (No module named 'smooth_basis')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/nose/failure.py", line 39, in runTest
    raise self.exc_val.with_traceback(self.tb)
  File "/usr/local/lib/python3.4/dist-packages/nose/loader.py", line 414, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python3.4/dist-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python3.4/dist-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/usr/lib/python3.4/imp.py", line 235, in load_module
    return load_source(name, filename, file)
  File "/usr/lib/python3.4/imp.py", line 171, in load_source
    module = methods.load()
  File "<frozen importlib._bootstrap>", line 1220, in load
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/donbeo/Documents/statsmodels/statsmodels/sandbox/gam_gsoc2015/tests/test_gam.py", line 1, in <module>
    from smooth_basis import make_poly_basis, make_bsplines_basis
ImportError: No module named 'smooth_basis'

======================================================================
ERROR: Failure: ImportError (No module named 'smooth_basis')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/nose/failure.py", line 39, in runTest
    raise self.exc_val.with_traceback(self.tb)
  File "/usr/local/lib/python3.4/dist-packages/nose/loader.py", line 414, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python3.4/dist-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python3.4/dist-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/usr/lib/python3.4/imp.py", line 235, in load_module
    return load_source(name, filename, file)
  File "/usr/lib/python3.4/imp.py", line 171, in load_source
    module = methods.load()
  File "<frozen importlib._bootstrap>", line 1220, in load
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/donbeo/Documents/statsmodels/statsmodels/sandbox/gam_gsoc2015/tests/test_smooth_basis.py", line 3, in <module>
    from smooth_basis import make_poly_basis, make_bsplines_basis
ImportError: No module named 'smooth_basis'

----------------------------------------------------------------------
Ran 91 tests in 21.727s

FAILED (SKIP=1, errors=2)
donbeo@donbeo-HP-EliteBook-Folio-9470m:~/Documents/statsmodels/statsmodels/sandbox$ 

3)如果我尝试从pycharm在python控制台中运行文件{例如gam_example,我收到以下错误

Backend TkAgg is interactive backend. Turning interactive mode on.
Traceback (most recent call last):
  File "<input>", line 3, in <module>
  File "/home/donbeo/Applications/pycharm-community-4.5.1/helpers/pydev/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: No module named 'smooth_basis'

我认为文件夹结构有问题或者我做错了什么。

什么是正确的解决方法?

我知道有些帖子建议做一个相对导入路径或将文件夹添加到python路径。但是通过这种方式,项目不能移植到其他系统,项目中的其他文件夹也没有这样的东西。

0 个答案:

没有答案