简体   繁体   中英

Pweave cannot import local module

I'm trying to import code from a local module into a pweave document, and I can't get it to work.

I have the following files

- foo.pmd 
- bar.py
- __init__.py

Contents of foo.pmd:

```python
import bar
bar.foobar()
```

Contents of bar.py:

def foobar():
    return(1)

Then I run pweave foo.md and the output fails:

---------------------------------------------------------------------------ImportError
Traceback (most recent call last)<ipython-input-1-1c3509f6dae7> in <module>()
----> 1 import bar
      2 bar.foobar()
ImportError: No module named
'bar'

I think this should work? Or am I trying to do something that's not possible?

I had the same problem. My solution:

import os
import sys
sys.path.append(os.getcwd())
import myfantasticmodule

I think the problem is, that the current working directory is not in the Python-Path where Python looks for modules (I have no idea why this problem only occurs with pweave).

os.getcwd()

gives you the "current working directory" and

sys.path.append()

adds this directory to the Python-path (only for this session! So you don't have to delete it at the end).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM