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

Restructure the Project to Follow Best Python Packaging Practices #23

Merged
merged 29 commits into from
Oct 26, 2023

Conversation

jmwright
Copy link
Member

This project was originally structured with just PyInstaller in mind, but now that it can be installed via pip, the repo needs to be restructured.

@justbuchanan
Copy link
Member

I installed with pip install git+https://github.com/cadquery/cq-cli@87e604e and I see a couple issues:

  • Trying to run cq-cli gives "ModuleNotFoundError: No module named 'cq_cli'"
  • cq_cli.py doesn't show up anywhere under site-packages/ and cqcodecs is placed directly inside site-packages/ rather than being under "cq_cli"

I messed around with this for a bit and I think I have it working now. I changed pyproject.toml

 [tool.hatch.build.targets.wheel]
-packages = ["src/cq_cli/cqcodecs"]
+packages = ["src/cq_cli"]

and now things show up correctly in site-packages/. After that, imports had to be changed from import cqcodecs... to import cq_cli.cqcodecs.... That got everything working for pip-installation, but broke the tests. When running cq_cli.py from within the project manually or for tests, import cq_cli.cqcodecs gave NoModuleFound errors. I was able to fix that by renaming cq_cli.py to main.py (it seems having a file and a directory named the same was confusing imports...) and by adding a sys.path hack to the top of the new main.py. I'm not super happy about the hack, but it does work and I can't figure out a way around it. I also see many answers online (stack overflow mostly) recommending it, so ¯\_(ツ)_/¯

@jmwright
Copy link
Member Author

Thanks for fixing that @justbuchanan

I fixed the PyInstaller spec file and added a few more dependencies to make sure that CadQuery doesn't end up broken when doing a pip install from git.

I created a clean environment and installed via pip install git+https://github.com/CadQuery/cq-cli.git@restructure and the following worked fine.

  • cq-cli --infile /home/jwright/repos/baby-auv/mech/components/auv_hull.py --outfile /tmp/hull.step
  • python -m cq_cli.main --infile /home/jwright/repos/baby-auv/mech/components/auv_hull.py --outfile /tmp/hull.step

It had an added advantage that it picked up the recent CadQuery change that suppresses the verbose STEP export info from OCCT.

Have you noticed any other issues, or are we ready to merge this?

@justbuchanan
Copy link
Member

Looks good!

One minor suggestion - you probably don't need to add the extra dependencies in pyproject.toml (commit e19e7c2). Pip will already get those for you automatically. In general, you should only list direct dependencies and let pip figure out transitive dependencies. It looks like path and cadquery are the only direct deps.

@jmwright
Copy link
Member Author

One minor suggestion - you probably don't need to add the extra dependencies in pyproject.toml

After I switched to the git install with pip the dependencies weren't getting installed, so I added those. I'll try a fresh install without them again, but will put them back if I have the same problem. The problem should go away with release packages of CadQuery.

@jmwright
Copy link
Member Author

Yeah, the CadQuery dependency installation does not work properly when installing from git for some reason. It could have something to do with this repo using pyproject.toml and CadQuery still using setup.py.

$ pip install git+https://github.com/CadQuery/cq-cli.git@restructure
Collecting git+https://github.com/CadQuery/cq-cli.git@restructure
  Cloning https://github.com/CadQuery/cq-cli.git (to revision restructure) to /tmp/pip-req-build-ovpfld5a
  Running command git clone --filter=blob:none --quiet https://github.com/CadQuery/cq-cli.git /tmp/pip-req-build-ovpfld5a
  Running command git checkout -b restructure --track origin/restructure
  Switched to a new branch 'restructure'
  Branch 'restructure' set up to track remote branch 'restructure' from 'origin'.
  Resolved https://github.com/CadQuery/cq-cli.git to commit addae4923ee9a22b4a3a734aa68ea7c38c88ebad
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting cadquery@ git+https://github.com/CadQuery/cadquery.git (from cq_cli==2.3.0)
  Cloning https://github.com/CadQuery/cadquery.git to /tmp/pip-install-9cnbtl8b/cadquery_caae7abfc3df4fbd9f59c040ea2e11ed
  Running command git clone --filter=blob:none --quiet https://github.com/CadQuery/cadquery.git /tmp/pip-install-9cnbtl8b/cadquery_caae7abfc3df4fbd9f59c040ea2e11ed
  Resolved https://github.com/CadQuery/cadquery.git to commit cce60ce97f583730fdfa4844f262d6168b8b63b1
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: cq_cli, cadquery
  Building wheel for cq_cli (pyproject.toml) ... done
  Created wheel for cq_cli: filename=cq_cli-2.3.0-py3-none-any.whl size=22123 sha256=2ad317fc5d158daafe8dc354aab5881863af35302a2a3d52f3ad20b89f2ca311
  Stored in directory: /tmp/pip-ephem-wheel-cache-1n09w88u/wheels/fe/a8/a6/b8966239398b2c47e87f2d32b7f848d6df165d981aab1b188c
  Building wheel for cadquery (setup.py) ... done
  Created wheel for cadquery: filename=cadquery-2.4.0.dev0-py3-none-any.whl size=150362 sha256=adef4cd7dc85e94a98d2792d63e2de4985ace5af11c600199ea0e62fdea05387
  Stored in directory: /tmp/pip-ephem-wheel-cache-1n09w88u/wheels/f6/bd/11/9f4461324b9afde1e7af02edd50dd6c7ce6613e86665b55920
Successfully built cq_cli cadquery
Installing collected packages: cadquery, cq_cli
Successfully installed cadquery-2.4.0.dev0 cq_cli-2.3.0
$ cq-cli -h
Traceback (most recent call last):
  File "/home/jwright/mambaforge/envs/cq-cli/bin/cq-cli", line 5, in <module>
    from cq_cli.main import main
  File "/home/jwright/mambaforge/envs/cq-cli/lib/python3.10/site-packages/cq_cli/main.py", line 15, in <module>
    import cadquery as cq
  File "/home/jwright/mambaforge/envs/cq-cli/lib/python3.10/site-packages/cadquery/__init__.py", line 10, in <module>
    from .occ_impl.geom import Plane, BoundBox, Vector, Matrix, Location
  File "/home/jwright/mambaforge/envs/cq-cli/lib/python3.10/site-packages/cadquery/occ_impl/geom.py", line 5, in <module>
    from OCP.gp import (
ModuleNotFoundError: No module named 'OCP'

@jmwright
Copy link
Member Author

Merging - Thanks for all your work on this @justbuchanan

@jmwright jmwright merged commit 3dd2e8d into main Oct 26, 2023
3 checks passed
@justbuchanan justbuchanan deleted the restructure branch October 28, 2023 19:22
@justbuchanan
Copy link
Member

Great, thanks for getting this merged in!

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

Successfully merging this pull request may close these issues.

2 participants