pyvenv.cfg
;os.py
marker);venv
module with the -m
key, or use the pyvenv built-in script: pyvenv /path/to/new/venv
c:\Python33\python -m venv /path/to/new/venv
cd /path/to/new/venv . bin/activate python3 some_script.py
/path/to/new/venv/bin/python3 some_script.py
#!/usr/bin/env python3
, for them you still need to, as before, do the activation. The solution is - about it just below.--upgrade
key: pyvenv --upgrade /path/to/new/venv
venv.EnvBuilder
, this class is written so that it can be expanded.distribute
, pip
and the necessary initial dependencies from requirements.txt
when initializing the environment. It is better to leave more complicated logic on the conscience of more tools designed for this, such as buildout or make, but the initial setup can be done at the EnvBuilder level.create(self, env_dir)
method is used, in the source class it looks like this: def create(self, env_dir): env_dir = os.path.abspath(env_dir) context = self.ensure_directories(env_dir) self.create_configuration(context) self.setup_python(context) if not self.upgrade: self.setup_scripts(context) self.post_setup(context)
ensure_directories
), configuration ( create_configuration
), adding python binaries ( setup_python
) and adding activation scripts ( setup_scripts
).post_setup
hook is post_setup
, into which you can add your actions. It can be seen that post_setup
is executed only when creating the environment, and during - upgrade
it will not be executed. This is easy to fix by adding another hook: class ImprovedEnvBuilder(venv.EnvBuilder): def create(self, env_dir): """Overwrite create method (add more hooks)""" env_dir = path.abspath(env_dir) context = self.ensure_directories(env_dir) self.create_configuration(context) self.setup_python(context) if not self.upgrade: self.setup_scripts(context) self.post_setup(context) else: self.post_upgrade(context) def post_upgrade(self, context): pass
ensure_directories
, the context
will be passed to an object containing all necessary information about the created environment in the form of attributes. For some reason, the documentation does not yet describe these keys, but you can easily understand everything yourself by looking at the code of the ensure_directories
method in the base class. I will cite the most useful attributes: import subprocess import venv class MyEnvBuilder(venv.EnvBuilder): def post_setup(self, context): script = '/path/to/some_script.py' subprocess.call([context.env_exe, script])
#/usr/bin/env python3
and use, remembering to do . bin/activate
. bin/activate
. If you are satisfied with this approach, then you can continue to use it in venv
.install_scripts(self, context, path)
method is implemented, which automates the copying of scripts and binaries to the created environment. In path
you must pass the path to a directory with nested subdirectories “common”, “nt”, “posix”, etc. In the subdirectory, in turn, put the necessary scripts or binaries. In “common” scripts for all platforms, in “nt” - for Windows, “posix” - for Linux, Mac OS X and other posix systems.__VENV_DIR__
__VENV_NAME__
__VENV_BIN_NAME__
__VENV_PYTHON__
#!__VENV_PYTHON__ import sys import my_module if __name__ == '__main__': sys.exit(my_module.run(sys.argv))
__VENV_PYTHON__
will be replaced with the full path to the python interpreter in the virtual environment.install_scripts
, it can be launched without the need to activate the environment through bin / activate.Source: https://habr.com/ru/post/157287/
All Articles