Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency file is not a file: ./requirements.txt #75

Open
Vuizur opened this issue Jan 9, 2024 · 10 comments
Open

Dependency file is not a file: ./requirements.txt #75

Vuizur opened this issue Jan 9, 2024 · 10 comments

Comments

@Vuizur
Copy link

Vuizur commented Jan 9, 2024

Thanks a lot for developing this project! I am very excited to try it.

I wanted to use it with a simple example project using poetry. I am trying to do it on Windows and have the following powershell file (build_with_pyapp.ps1).

# Export poetry requirements to requirements.txt
poetry export -f requirements.txt --output requirements.txt
# Now set environment variables properly in powershell
$env:PYAPP_PROJECT_VERSION="0.1.0"
$env:PYAPP_PROJECT_NAME="pyappexample"
$env:PYAPP_PROJECT_DEPENDENCY_FILE="./requirements.txt"
$env:CARGO_TARGET_DIR="./target" # For speeding up future builds
$env:PYAPP_EXEC_SCRIPT="./pyappexample/circle.py"
cargo install pyapp

Unfortunately I am getting the following error:

[<compiling stuff...>]
error: failed to run custom build command for `pyapp v0.13.0`

Caused by:
  process didn't exit successfully: `C:\Users\hanne\Documents\Programme\pyappexample\./target\release\build\pyapp-7c9fbca43121c7ae\build-script-build` (exit code: 101)
  --- stdout
  cargo:rustc-env=PYAPP_PROJECT_NAME=pyappexample
  cargo:rustc-env=PYAPP_PROJECT_VERSION=0.1.0

  --- stderr
  thread 'main' panicked at C:\Users\hanne\.cargo\registry\src\index.crates.io-6f17d22bba15001f\pyapp-0.13.0\build.rs:389:9:       


  Dependency file is not a file: ./requirements.txt


  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: failed to compile `pyapp v0.13.0`, intermediate artifacts can be found at `C:\Users\hanne\Documents\Programme\pyappexample\./target`.
To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path.
(pyappexample-py3.10) PS C:\Users\hanne\Documents\Programme\pyappexample> 

I am executing the powershell script from the root directory, where my requirements.txt file is located.

Have a great day!

@ofek
Copy link
Owner

ofek commented Jan 9, 2024

Sorry about that! I'll improve documentation but when you don't use the recommended installation mechanism file paths must be absolute.

@Vuizur
Copy link
Author

Vuizur commented Jan 10, 2024

Thanks a lot for the fast answer. That was in fact the reason. I agree that using cargo install is probably not the best, the resulting .exe will land in the Rust CLI tool directory, which is probably not what we want.

I created a new Powershell script that seems to work great. I would suggest adding it to the docs as an example, maybe it can be improved though. I think it would be great for beginners, I needed some time to get it to work 😅. One could maybe also make it compatible with Linux for easy cross-compilation.

# This is supposed to be executed in the root of the project

# Export poetry requirements to requirements.txt
poetry export -f requirements.txt --output requirements.txt
# Now set environment variables properly in powershell
$env:PYAPP_PROJECT_VERSION="0.1.0"
$env:PYAPP_PROJECT_NAME="pyappexample"
# Set the path to the requirements.txt file, it is the absolute path of ./requirements.txt
$env:PYAPP_PROJECT_DEPENDENCY_FILE="$((Get-Item -Path "./requirements.txt").FullName)"
$env:PYAPP_EXEC_SCRIPT="$((Get-Item -Path "./pyappexample/circle.py").FullName)"

if (!(Test-Path -Path "./pyapp-latest")) {
    # Download the zip file from the URL
    Invoke-WebRequest https://github.com/ofek/pyapp/releases/latest/download/source.zip -OutFile pyapp-source.zip
    Expand-Archive -Path ./pyapp-source.zip -DestinationPath .
    Move-Item -Path ./pyapp-v* -Destination ./pyapp-latest
    Remove-Item -Path ./pyapp-source.zip
}
# Change the current directory to the extracted folder
Set-Location -Path ./pyapp-latest
cargo build --release
# Move Item and rename it to $env:PYAPP_PROJECT_NAME.exe, overwrite if exists
Move-Item target\release\pyapp.exe ..\$env:PYAPP_PROJECT_NAME.exe -Force
# Change the current directory to the root of the project
Set-Location -Path ..

I haven't explored it beyond this toy example yet, but this seems significantly easier to use than pyinstaller so far, amazing that you created this.

@Vuizur
Copy link
Author

Vuizur commented Jan 10, 2024

I tested a bit more and discovered that one can't import functions from other .py files in the same folder as the main script. So if I have a parallel file helper.py with the function hello and try to import this from app.py, I will get ModuleNotFoundError: No module named helper. The same .py file is able to be executed by Python without problems.

@ofek
Copy link
Owner

ofek commented Jan 10, 2024

Yes the easiest solution for that is to turn your scripts into a package. You can then either publish and install from PyPI or if private just build and point to the path to the wheel: https://ofek.dev/pyapp/latest/config/#project-embedding

@Vuizur
Copy link
Author

Vuizur commented Jan 10, 2024

Thanks for the hint. My final version of the powershell script now finally works! (I think.)

It is here:

# This is supposed to be executed in the root of the project

# The executable will execute this function when it starts
$env:PYAPP_EXEC_SPEC="pyappexample.circle:run"

$executable_name = "pyappexample"

# We are building the wheel with poetry
if (Test-Path -Path "./dist") {
    Remove-Item -Path ./dist -Recurse -Force
}
poetry build
# Get the full path of the wheel file in the dist directory
$wheel_file = (Get-Item -Path "./dist/*.whl").FullName
# Print wheel file path
Write-Host "Wheel file path: $wheel_file"
$env:PYAPP_PROJECT_PATH=$wheel_file

# if check if pyapp-latest folder not exists
if (!(Test-Path -Path "./pyapp-latest")) {
    # Download the zip file from the URL
    Invoke-WebRequest https://github.com/ofek/pyapp/releases/latest/download/source.zip -OutFile pyapp-source.zip
    # Extract the zip file to the temporary folder using Expand-Archive
    Expand-Archive -Path ./pyapp-source.zip -DestinationPath .
    # Move the extracted folder to the desired location and rename it
    Move-Item -Path ./pyapp-v* -Destination ./pyapp-latest
    Remove-Item -Path ./pyapp-source.zip
}
# Change the current directory to the extracted folder
Set-Location -Path ./pyapp-latest
cargo build --release
# Move Item and rename it to $env:PYAPP_PROJECT_NAME.exe, overwrite if exists
Move-Item target\release\pyapp.exe ..\$executable_name.exe -Force
# Change the current directory to the root of the project
Set-Location -Path ..

@ofek
Copy link
Owner

ofek commented Jan 10, 2024

Nice!

@quinnhornblow
Copy link
Contributor

quinnhornblow commented Jan 12, 2024

Thanks for the powershell script, this is exactly what I was looking for!

When I try the single-project-embedded method or use this script I get this error:

  --- stdout
  cargo:rustc-env=PYAPP_PROJECT_DEPENDENCY_FILE=
  cargo:rustc-env=PYAPP__PROJECT_DEPENDENCY_FILE_NAME=

  --- stderr
  thread 'main' panicked at '

`; must only contain ASCII letters/digits, underscores, hyphens, and periods, and must begin and end with ASCII letters/digits.

Write-Host "Wheel file path: $wheel_file" prints a valid output to the .whl file.

Any ideas?

@Vuizur
Copy link
Author

Vuizur commented Jan 22, 2024

@quinnhornblow Does https://github.com/Vuizur/pyappexample work for you? If yes, the issue could be the executable name or the file path - does it contain any weird characters?

@quinnhornblow
Copy link
Contributor

@quinnhornblow Does https://github.com/Vuizur/pyappexample work for you? If yes, the issue could be the executable name or the file path - does it contain any weird characters?

Yes that does work. I've created a fork of your example to replicate my issue here. The main change is that I'm using setuptools instead of poetry. I don't really know how they differ, setuptools is just what we use where I work. Changes:

  1. Rewrite pyproject.toml to use setuptools.
  2. update line 12 in build_with_pyapp.ps1 to python -m build

When running build_with_pyapp.ps1 the .whl file builds successfully, but process fails at 'cargo build' with the ASCII error mentioned above. Although the process does succeed if I run in debug mode without the --release tag.

@agriyakhetarpal
Copy link

Maybe I'm beating around a dead bush, but something similar came up in uv recently, see: astral-sh/uv#2276, astral-sh/uv#1666.

poetry export might not be creating files with UTF-8 encodings, so I would advise ensuring that the correct encoding is being used for the file to be parsable. This may or may not fix the issue at hand here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants