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

[question] pyinstaller to build an exe #14219

Closed
1 task done
maitrey opened this issue Jul 4, 2023 · 9 comments
Closed
1 task done

[question] pyinstaller to build an exe #14219

maitrey opened this issue Jul 4, 2023 · 9 comments
Assignees

Comments

@maitrey
Copy link
Contributor

maitrey commented Jul 4, 2023

What is your question?

Dear Conan Folks,

My requirement is that I have a python tool that I need to build as installer. I would like to use virtualenv for it.
Is there a recommended approach that I could use?
I need to call the pyinstaller with --onefile buildspec

Could you please help me?

Have you read the CONTRIBUTING guide?

  • I've read the CONTRIBUTING guide
@memsharded
Copy link
Member

Hi @maitrey

Thanks for your question.
However, not sure what you mean. The pyinstaller.py in the root of the repo can create the self-contained executables. How the virtualenv relates to that? What do you mean with build as installer? Like a self extracting and self installing standalone app? Like the Windows installer we distribute in our downloads page? The --onefile makes things slower, what is exactly your use case? Isn't it enough to add the --onefile in the pyinstaller.py file?

@maitrey
Copy link
Contributor Author

maitrey commented Jul 5, 2023

So my usecase is : I have a tool written in python, This tool is an application in the end distributed as an exe(consumer are different projects) for which we are using pyinstaller. To create the exe we like to use virtualenv and install the required pip packages. This is because so that all developers when making changes to the application use the exact same set of pip packages. We are using pyinstaller with --onefile . Our idea is to create an exe and make a conan package and consume it in projects. Are we wrong with our approach?

@memsharded
Copy link
Member

But is it your application calling Conan under the hood, via the API or something? Or users will be calling Conan? If you are creating an exe of another tool, lets call it "mytool", you have just one mytool.exe that users can run? What do you mean with "make a conan package and consume it in projects"?

The part of you creating a mytool.exe from your Python app, with pyinstaller and using virtualenv, I think I understand that, but I still fail to see the connection between mytool.exe and conan.

@maitrey
Copy link
Contributor Author

maitrey commented Jul 5, 2023

mytool will be build by calling : conan build . --user autosar --channel featurebringup . Yes i create a python application mytool.exe and consumers of mytool.exe are projects that call this application at build time.

@memsharded
Copy link
Member

mytool will be build by calling : conan build . --user autosar --channel featurebringup . Yes i create a python application mytool.exe and consumers of mytool.exe are projects that call this application at build time.

Ok, now I understand a bit better the flow. conan build can build an application, typically a C++ compiled application. For building Python applications, that is a bit more complicated, because Conan itself is already running in a Python environment, and you cannot/shouldn't install extra stuff in the Conan virtualenv, there is a risk of breaking things. So it would be necessary that the build() contains instructions how to create a new virtualenv on the fly, do things there in that virtualenv, call whatever you want to call there (pyinstaller) and create an executable as a result. In that regard, Conan is not aware of that, whatever is done in the build() method, the recipe author is fully responsible for it. If the build() method creates a mytool.exe that is fully self contained, then it shouldn't be an issue.

But the main problem would be creating and managing the virtualenv on the fly, if you can do it safely, then I don't see a strong reason against it.

Note: there is some work, for 2.X roadmap to try to help in the creation of virtualenvs for Conan packages, maybe you want to have a look: #11601

@maitrey
Copy link
Contributor Author

maitrey commented Jul 5, 2023

Ah ok. I am exactly doing the way that you already mentioned, calling this in build() method:

from venv import create
from os.path import join,  abspath
from subprocess import run

dir = join(self.build_folder, "venv")
create(dir, with_pip=True)

run(["Scripts/pip", "install", "-r" , abspath("requirements.txt")], cwd=dir)

However the requirements are getting installed not in the virtual environment but in the Python installation directory.

@memsharded
Copy link
Member

I don't know enough about how it works, but I think it is not enough to launch scripts in the cwd of the virtualenv, but it needs to be activated first, but I don't know how to do that programatically.

@maitrey
Copy link
Contributor Author

maitrey commented Jul 10, 2023

Hi @memsharded ,
I have used a venv_create.bat script with the following contents:

python -m venv venv && venv\Scripts\activate && pip install --upgrade setuptools && pip install -r requirements.txt

And calling this script in the build method of the recipe:

self.run("venv_create.bat")
self.run("venv\Scripts\pyinstaller.exe --onefile build\Build.spec")
self.run("venv\Scripts\deactivate")

This seems to work, many thanks!
Could you please close the ticket?

@memsharded
Copy link
Member

Sure, thanks for following up and the feedback!

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

No branches or pull requests

2 participants