I am using macOS and there I have created pip env. After I activate pip env, I install ansible in created virenv. After I activate this virenv terminal, I run vagrant up in this terminal.
Is this good practice ?
My motivation to do this:
I want to avoid whole mess with pip packages and version on macOS. I use vagrant for testing my ansilbe playbooks.
He wants Ansible to run in the virtual environment created using Pipenv not on one of the target (testing) virtual machines. Thus, he can port his entire virtual environment to any machine and in theory it is clean from the OS Python, etc. packages while being exactly reproducible as code. The Vagrant VMs are only for testing machines to run the playbooks against before going on to another development or production platform.
Related
I have a virtualenv called env on my directory...After activating:
source env/bin/activate
I see that it is activated...(env)
I have installed some libraries on the virtual env, and i can see that they ares installed on their folders, but calling...
pip list
The libraries that Appears on the list are not the libraries installed on the env.
Also calling a python file to execute, dontn run.
Therefore calling for checking the python version, don't match with the env python version.
I have tried to reinstall python and pip, but nothing happens
I work on a 2 macOS machines and I use iCloud for sharing my projects. I created the env on one macOS machine but the same env didn't work on the other macOS machine. That was the problem.
I created a requirements.txt on the first macOS machine, then I created a new env on the other macOS machine, loaded the requirements.txt, and it now works.
I am using Ansible from a pipeline agent, to configure Ubuntu VMs and I would like to use the azure_rm_storageblob and mssql_script module directly on the Ubuntu VM that I am configuring. I had some issues running this, because the packaging module was not installed on the hosts.
Is there a way to install the pip modules only for use when I run Ansible (maybe a virtual environment), as I don't want to mess with the pip modules that are installed on the server for other purposes.
If this is done using something like a python virtual env, how do I make sure that this is used when I connect to the VM using Ansible?
You have to use virtual environments to solve that problem (link to the python doc)
First you create the environment with :
python -m venv venv
Then depending on your OS, you have to activate you virtual environment, on Unix you can do :
source venv/bin/activate
The name of you virtual env should be written in your terminal, at that point you know you are using it and not the default python env.
I am trying to create a virtual environment to run a script which requires Python 3.6. I started off with Pipenv but I am unable to create the same environment on other platforms via the Pipfile.lock or requirements.txt unless the other platform(s) has Python 3.6 installed. I have read this post but I am unsure which direction I should take to create a virtual environment which can be shared and run its own version of Python independent of operating system and version of Python installed on the other platform.
Virtual environments are not portable, they depend on the Python installation you have.
You can't share/distribute virtual environment with others, because you can't control which version of Python others are using.
If you want to distribute your code along with all dependencies including the specific version of Python interpreter, you can use PyInstaller. It is far from perfect and little bit hacky. Also it generates a package which is specific to operating system.
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
There is also a detailed step-by-step guide on how to use PyInstaller.
https://realpython.com/pyinstaller-python/
This is step-by-step how I use Python virtual environment and share it with co-workers.
To check python and virtualenv presence, run following commands:
which python3
python3 -m pip list | grep env
which virtualenv
Install a python virtual environment builder:
python3 -m pip install virtualenv
Create a virtual environment named venv inside the project's directory: virtualenv venv
To activate this environment use this command inside project's directory: source venv/bin/activate
Install python modules dependencies listed in a requirements.txt:
python3 -m pip install -r requirements.txt
You should activate virtual environment when you working with python in this directory for package installation and for running commands in the project directory. When you need to deactivate the virtual environment do it using deactivate command.
To deactivate environment simply run: deactivate
I want to distribute a python script with external requirements, but I don't want to clutter up the users machines. Can I my script activate a virtual environment, and install its requirements to said VE.
Additionally, is there a way to have the VE destroy itself once the script in finished executing.
You do not need to explicitly activate the virtual environment. If the virtual environment is located at /path-to-venv, then executing:
/path-to-venv/bin/pip install package
will install package into the virtual environment. Likewise, running the Python interpreter located at /path-to-venv/bin/python will cause packages to be loaded from the virtual environment located at /path-to-venv without the need for an explicit activation.
I have a virtual environment I have created on an Ubuntu virtal machine I am hosting on a windows PC. I intend to replicate my virtual machine in my virtal environment on the virtual machine. However, when trying to install modules to the VE I get a messgae saying that they are already installed - they're not installed in the VE but are on the VM. I thought when set active the VE would have no context of the VM which hosts it?
I have downloaded virtualenvironment sudo pip install virtualenv and then created a virtual environment sudo virtualenv virtual_environment. I then set the virtual environment to active source virtual_environment/bin/activate
When I try and do an apt-get install I get the message 0 upgraded, 0 newly installed, 0 to remove and 202 not upgraded despite the fact I have no modules whatsoever on the VE.
What am I doing wrong?
Thanks!
I think you're getting a bit confused about what virtualenv does. It is only for isolating Python files and libraries (those you install with pip install). It does nothing for your operating system files (those you install with apt-get).
If you want to create a re-usable container of operating system files (with apt-get) then look instead at something like Docker.