hyankov
hyankov

Reputation: 4130

Scripting Python dev env setup

I have created a collection of Python scripts, to be used as tools in our dev group. The scripts have dependencies, such as requests and flake8 (this should technically be under dev setup, but whatever).

I have created a setup-dev.bat file, with the idea of being a "first-use" setup.

python -m pip install --upgrade pip --user
python -m pip install --upgrade virtualenv --user
python -m venv .env
call .\.env\Scripts\Activate.bat
python -m pip install --upgrade pip --user
python -m pip install --upgrade virtualenv --user
pip install .

The idea is:

  1. Install and/or upgrade pip on the system

  2. Install and/or upgrade virtualenv on the system

  3. Create a virtual environment .env

  4. Activate it

  5. Upgrade the pip and virtualenv under it (not sure if this makes sense at all)

  6. 'Install' the scripts - most importantly, this install the dependencies

Now the scripts are ready to use and their dependencies are contained within .env. The idea is - if/when those scripts get distributed to the higher environments for Ops to run, I don't want to pollute their environment with my dependencies.

I should point out that those tools will never be distributed over pip or any other system, they are for internal use within the company and will be hosted on a source control, or emailed to Operations with execution instructions.

Now my questions:

  1. Does this even make sense?
  2. Have I unintentionally created a dependency on Windows? In step 4 above I should probably run just \activate instead of the .bat? I guess the .env depends on the host system and would have different contents under a different OS? Which script do I have to call to activate the environment in most OS-agnostic way? The activate, the ps1 or the .bat?
  3. After the users of my scripts have set it up for first time, I guess they should be educated that if they want to run the scripts, they'd first have to run .env\Scripts\activate? Is there any way to semi-automate this, make it more obvious or fool-proof?
  4. Why do I get You are using pip version 19.0.3, however version 19.3.1 is available. as a result of my pip upgrade command?!

Edits:

I feel like I am progressing, but I am confused about the lack of posts around such setup and it feels like I am doing something wrong.

Upvotes: 3

Views: 443

Answers (1)

You could bundle the scripts in a wheel file (read about entry_points here) that can be installed via pip (i.e., pip install bundle.whl; see this). With this approach, the dependencies will be part of the wheel file and pip will automatically install the dependencies when the wheel file is installed.

--user flag to pip will install the scripts in a location local to the user as determined by the user scheme. So, if you are fine with the scripts "polluting" a user's local installation, then virtualenv is not needed.

Upvotes: 1

Related Questions