简体   繁体   中英

How can I set my path so that it works with Python code that is locally installed as a package

I am developing a set of python files that should work together as a package. The file has the following structure.

    PyFinances
    ├─.venv
    ├─LICENSE
    ├─Makefile
    ├─README.rst
    ├─requirements.txt
    ├─setup.py
    ├─test
    │  ├─test_pyfinances.py
    │  └─read_files.py
    └─PyFinances
       ├─pyfinances.py
       └─read_files.py

The header and the first function in the pyfinances.py file looks like this

import numpy as np
import pandas as pd
from read_files import ReadRunOptionsFile

def pyfinances():
    "generic content here"
    return 25.0

I successfully installed the code in a virtual environment with the following two commands

pip3 install wheel
pip3 install .

Which yield the following response

Successfully installed PyFinances-0.1.0

To test the package install, I cd 4 or 5 directories away from where the source code is, create a file titled test.py with the following import statement at the top of the file

from PyFinances.pyfinances import pyfinances

The import seems to have worked because it does not tell me that it does not recognize PyFinances, but instead it does not recognize the read_file module and I get the error.

ModuleNotFoundError: No module named 'read_files'

How do I fix this issue?

Well... You didn't import _read_files , you imported pyfinance . Here's what you probably should do:

import PyFinances.pyfinances as pfy
import PyFinances.read as pfr

For this to work, I should note, you must remove the underscores from the beginning of your source filenames (and edit setup.py accordingly). The way you have them named would only allow for the use of a from... import... , and no import... as...

And, you're also missing __init__.py in every directory. It does not require any code in it, you just need it to exist in PyFinances/ , PyFinances/test/ , and PyFinances/PyFinances/ .

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