diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index edbdd3f17c..0fb32720ab 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -184,3 +184,52 @@ jobs: which node node -v node src/lpython/tests/test_lpython.js + + test_pip_pkgs: + name: Test PIP Installable Packages + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - uses: mamba-org/provision-with-micromamba@v15 + with: + environment-file: ci/environment.yml + extra-specs: | + bison=3.4 + + - uses: hendrikmuhs/ccache-action@main + with: + variant: sccache + key: ${{ github.job }}-${{ matrix.os }} + + - name: Setup Platform (Linux) + shell: bash -l {0} + run: | + echo "LFORTRAN_CMAKE_GENERATOR=Unix Makefiles" >> $GITHUB_ENV + echo "WIN=0" >> $GITHUB_ENV + echo "MACOS=0" >> $GITHUB_ENV + echo "ENABLE_RUNTIME_STACKTRACE=yes" >> $GITHUB_ENV + + - name: Build Linux + shell: bash -l {0} + run: | + xonsh ci/build.xsh + + - name: PIP install required packages + shell: bash -l {0} + run: | + python -m pip install lptypes lpynn numpy + + - name: Test PIP Pacakges with Python + shell: bash -l {0} + run: | + python integration_tests/test_pip_import_01.py + + - name: Test PIP Pacakges with LPython + shell: bash -l {0} + run: | + pip_pkg_path=$(python -c "import site; print(site.getsitepackages()[0])") + echo $pip_pkg_path + ./src/bin/lpython integration_tests/test_pip_import_01.py -I $pip_pkg_path diff --git a/integration_tests/test_pip_import_01.py b/integration_tests/test_pip_import_01.py new file mode 100644 index 0000000000..7ce361707d --- /dev/null +++ b/integration_tests/test_pip_import_01.py @@ -0,0 +1,39 @@ +from lpynn.perceptron import init_perceptron, print_perceptron, normalize_input_vectors, Perceptron, train_dataset +from lptypes import i32, f64 + +def main0(): + p: Perceptron = Perceptron(0, [0.0], 0.0, 0, 0.0, 0.0, 0) + init_perceptron(p, 2, 0.05, 10000, 90.0) + print_perceptron(p) + print("=================================") + + input_vectors: list[list[f64]] = [[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0]] + outputs: list[i32] = [1, 1, 1, -1] + + normalize_input_vectors(input_vectors) + train_dataset(p, input_vectors, outputs) + print_perceptron(p) + print("=================================") + + assert p.cur_accuracy > 50.0 + assert p.epochs_cnt > 1 + +def main1(): + p: Perceptron = Perceptron(0, [0.0], 0.0, 0, 0.0, 0.0, 0) + init_perceptron(p, 2, 0.05, 10000, 90.0) + print_perceptron(p) + print("=================================") + + input_vectors: list[list[f64]] = [[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0], [1.5, 1.0]] + outputs: list[i32] = [1, 1, -1, 1, -1] + + normalize_input_vectors(input_vectors) + train_dataset(p, input_vectors, outputs) + print_perceptron(p) + print("=================================") + + assert p.cur_accuracy > 50.0 + assert p.epochs_cnt > 1 + +main0() +main1() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index e7511fa1a6..6396b539b2 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -202,8 +202,8 @@ Result get_full_path(const std::string &filename, return file_path; } else { // If this is `lpython`, do a special lookup - if (filename == "lpython.py") { - file_path = runtime_library_dir + "/lpython/" + filename; + if (filename == "lpython.py" || filename == "lptypes.py") { + file_path = runtime_library_dir + "/lpython/" + "lpython.py"; status = read_file(file_path, input); if (status) { lpython = true; @@ -3622,19 +3622,10 @@ class SymbolTableVisitor : public CommonVisitor { in any directory other than src/runtime will also be ignored. */ - if (mod_sym == "lpython") { + if (mod_sym == "lpython" || mod_sym == "lptypes" || mod_sym == "numpy") { return ; } - /* - If import_path is not empty then insert it - in the second priority. Top priority path - is runtime library path. - */ - for( auto& path: import_paths ) { - paths.push_back(path); - } - /* Search all the paths in order and stop when the desired module is found. @@ -3696,7 +3687,13 @@ class SymbolTableVisitor : public CommonVisitor { if (!t) { std::string rl_path = get_runtime_library_dir(); SymbolTable *st = current_scope; - std::vector paths = {rl_path, parent_dir}; + std::vector paths; + for (auto &path:import_paths) { + paths.push_back(path); + } + paths.push_back(rl_path); + paths.push_back(parent_dir); + if (!main_module) { st = st->parent; } @@ -3746,7 +3743,12 @@ class SymbolTableVisitor : public CommonVisitor { void visit_Import(const AST::Import_t &x) { ASR::symbol_t *t = nullptr; std::string rl_path = get_runtime_library_dir(); - std::vector paths = {rl_path, parent_dir}; + std::vector paths; + for (auto &path:import_paths) { + paths.push_back(path); + } + paths.push_back(rl_path); + paths.push_back(parent_dir); SymbolTable *st = current_scope; std::vector mods; for (size_t i=0; i