How to include python==3 with virtual environment of python module - python

I am creating python bundle for a client. I want to add python==3.7 also with that bundle so the user need not to install python on windows platform.
Created Virtual environment and copied that to new pc (without python install) and opened python from venv/Script/Python.exe.
But got error of missing dll files
I expect a bat file containing python.exe address from venv folder and running python file using venv python.exe or something like this

Related

Is it possible to have two distinct install of Python 3 of the same revision on a Windows system?

I know it possible to have two installs of Python of different versions on a Windows system. But I cannot manage to have two installs of the same revision (in my case 3.8.10) to coexist.
I'm designing an application that creates a Python process. That process needs to run from a specific version of Python with packages of specific versions installed on it. In order to fully control the Python install, decision was made to install it inside the application distribution directory, segregating it from any other Python installed on the system. No environment variable refers to it.
As part of the the deployment/install process for the application, a PowerShell script downloads the Python installer and installs Python and the necessary packages into the application distribution directory. The Python installer is invoked as follows:
.\\python-3.8.10-amd64.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0 TargetDir="$curDir\\Python" Include_exe=1 Include_lib=1 Include_pip=1 Include_tcltk=1 | Out-Null
It works well unless the system has already a Python install of the same version installed on it. In that case, running the installer will break the existing install, and not fully install the new one.
I tried to run the installer manually and I noticed that it is able, somehow, to detect that an install of the same revision exist on the system. In that case, it does not allow an new install. To do so, I would have to uninstall Python at its current location to be able to install it somewhere else.
Is there a way to have two distinct installs of Python 3 of the same revision on a Windows system? And if yes, how can it be done?
A better aproach instead of installing python again would be using virtual environments.
To create a new python env. Open the command line (Powershell) on Windows and navigate to the directory you want your python env to be.
Type python3 -m venv tutorial-env. This will create a new python virtual env named tutorial-env
To activate that env on Windows powershell type: tutorial-env\Scripts\activate.bat
To deactivate the env type deactivate
If you are wondering what python virtual environments do. They basically do what you are trying to do but without installing python globally again. When you create a new python env, a new python3 is placed in your env directory, in this case in the tutorial-env directory, and when you activate the environment, it replaces the python global path to the path in your env (in this case in tutorial-env). Now when you are on this virtual env and install new python packages, they will only be available when you activate that env.
For more information about virtual environments please refer to Python official docs.

How exactly works Python venv folder? My project use the dependencies into this venv folder executing the project using a different Python version?

I am an absolute beginner with Python (I came from Java) and I have the following doubt about the venv folder in my project.
So basically I have this project structure:
As you can see it contains the venv folder. Reading on the Python official documentation:
https://docs.python.org/3/library/venv.html
I can read:
The venv module provides support for creating lightweight “virtual
environments” with their own site directories, optionally isolated
from system site directories. Each virtual environment has its own
Python binary (which matches the version of the binary that was used
to create this environment) and can have its own independent set of
installed Python packages in its site directories.
So from what I can understand it means that, using venv, every project have its own venv folder containing:
The bin folder: it contains the Python interpreter binary (in this specific case Python 3.7).
The lib folder: containing the dependencies that I have installed using pip3 (infact I installed scapy and scapy_http via pip3)
This because my PyCharm IDE is set to use Python 3.7
If this reasoning is correct (I am asbolutly not sure) it means that when I run something like:
python3 packet_sniffer.py
the packet_sniffer.py script (contained in the previous project) will be run using the Python 3 bynary and the dependencies that are into my venv folder. Is it correct?
If my reasoning is correct I have the following doubt:
What happen when I run it using Python 2 by
python packet_sniffer.py
I suppose that it is not using anymore the Python 3 version embedded into the venv folder of my project. And what dependencies of scapy am I using? where does it come from?
As you can see running with python instead python3 I am using Python 2 version:
root#kali:~# python --version
Python 2.7.17
root#kali:~# python3 --version
Python 3.7.5
If you create a virtual environment, and you then activate it, the python interpreter installed in this virtual environment will be run if you call any of these: python foo.py / python3 foo.py. There will be NO differences between calling python foo.py or python3 foo.py (if you installed python3 of course).
The lib folder will contain the associated dependencies that you install through pip in this virtual environment and when you use python interpreter through this virtual environment you will use the aforementioned dependencies
So from what I can understand it means that, using venv, every project have its own venv folder containing:
The bin folder: it contains the Python interpreter binary (in this specific case Python 3.7).
The lib folder: containing the dependencies that I have installed using pip3 (infact I installed scapy and scapy_http via pip3)
Using venv every project has its own virtual environment folder that you can name anything you want, containing yes the bin folder and the lib folder.
python virtual environments
The venv folder is a suggested standard for installing python virtual environments.
To use the virtual environment you must first activate it.
Note: Activate your python virtual environment prior to installing any packages required by your application.
from your command line execute:
source venv/bin/activate
python packet_sniffer.py
Note: You do not need to specify the specific version of python as it defaults to the version used to create the virtual environment.
PyCharm can be configured to utilize python virtual environments
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
If you have the virtual environment activated then everything you say about executing python3 is correct. However, all other commands are still available and will run from wherever they are located assuming they can be found by your shell. So your python command is probably running the system-level installed version of python.
If you want to know exactly where that python interpreter is running from:
python
>>> import sys
>>> print(sys.executable)
C:\Python27\python.exe

Virtualenv won't create environment; runtime error

I'm trying to create a simple virtualenv environment; python and virtualenv are both installed. When I go in to my directory in which I am trying to make this environment, I keep getting a Windows Script Host popup box:
Script: *My path to virtualenv.js*
Line: 1
Char: 1
Error: Object expected
Code: 800A138F
Source: Microsoft JScript runtime error
I went in to my file to see where the problem may have lied and assumed it was just a module I hadn't installed yet, though all of them are in the node_modules in my package. I'm not sure where else this issue is coming from?
It looks to me as if you have a virtualenv.js JavaScript file on your path, and that JS files are associated with Windows Scripting Host on your machine. (Perhaps you have the node-virtualenv project installed, which puts a JavaScript file with the name virtualenv.js in your path?)
To create a typical Python virtual environment, you want to invoke the (Python) virtualenv.py script. Assuming you've installed virtual environment with:
pip install virtualenv
Then you can use:
python -m virtualenv ENV
to invoke the Python interpreter directly, and have it load the appropriate virtualenv.py script file on your path.

Copy Python project folder to computer without python, can I run it?

So I develop a python application and I plan to copy the whole folder for my friend to use it as end-user.
But my friend does not have python installed in the computer and I don't want to make them install it since he is not a developer.
In my project I have set up the virtualenv with python.exe inside it but without the site-packages, and I copy the virtualenv together with the project folder.
Is it possible to do this kind of setup so the application in the other end runs without python installed?
virtualenv is a good option if you are transferring the folder between two same operating systems.
In order to include the correspond site packages that are already installed in your computer, install them inside the virtualenv context by doing pip install in the virtualenv shell.
You could use pip freeze to get a list of installed python packages from your computer.
You could then include a .bat file (if it is a windows system) or .sh file (if its a linux system) so it would run your script with the virtualenv context.

Where is pyvenv script in Python 3 on Windows installed?

After reading the following statement from PEP 405
A pyvenv installed script is also provided to make this more
convenient:
pyvenv /path/to/new/virtual/environment
I tried to create a new virtual environment and failed miserably;
C:\>python --version
Python 3.3.1
C:\>pyvenv myvenv
'pyvenv' is not recognized as an internal or external command,
operable program or batch file.
Apparently pyvenv script is not installed into Scripts folder which is being usually added to the PATH environment variable making it possible to easily run such scripts on the command line.
Is PEP 405 wrong, was it not properly implemented in Python 3.3 or am I missing something?
It looks like pyvenv script is placed in Tools\Scripts subfolder inside Python installation folder (sys.prefix). It seems like copying it to Scripts subfolder is a good idea as it allows to simply type pyvenv from the command line (assuming Scripts folder is already on the PATH). As there's no exe wrapper for this script one has to make sure
.py extension is added to PATHEXT environment variable so that
Windows finds Python script placed on the PATH when typing script's
name at the command prompt.
.py extension is associated either with Python executable or with Python launcher (py.exe) which is available starting from Python 3.3
Alternatively one can just type python -m venv instead of pyvenv and save himself all of the hassle...
Related Python bug 17480 - pyvenv should be installed someplace more obvious on Windows
Use python -m venv someenvname instead.
Moreover, there is no strong reason to add python folder to PATH if you use system-wide python.exe only for creating virtual environments.

Categories