This is a XY-problem. Let me first tell how you can solve your problem, and then explain in the Appendix what was the reason for the problems you were facing.
1) Solving the problem: Creating portable application
Your original problem is in the first paragraph: You want to share your python script/app with someone, and not to force the user to install python. Virtual environments are not solution to your problem. Although, they are part of the solution since any application development should be done inside a virtual environment to make your projects isolated. Virtual environments are not portable; they are dependent on the python installation on the machine they were created on.
If you want to share your code, you have three different options
(1) Creating and executable
Many people like to use tools like cx_Freeze, pyinstaller or Nuitka to create an executable from the python code. There are gotchas, though. These tools work quite well with simple apps, but can be a pain with larger and more complex applications that use lot of static files, extension modules (.dlls), or depend on the __file__
attribute of the module to access a file on filesystem. Just do a simple search on SO with the toolname to see some example cases.
(2) Sharing code with portable python
There are portable python versions. For example WinPython, or the Embeddable Python from Python.org. You could pack them with your application code and simple .bat
file that runs the python.exe myscript
(using relative paths). If you have external dependencies (some packages), you could include their .whl
files (download from PyPI), and make your .bat
install them, too. I have done this successfully previously with very complex python programs, and handled my programs to end-users that have no clue about python. This takes a bit of effort and manual work, though.
(3) Using pynsist
An alternative approach would be to use pynsist for the job. It is used for distributing python apps, but it does not make a single "myapp.exe". What it does is basically automates the "Sharing code with portable python". It also creates an installer that you can just handle to your friend(s) to use, and the installed program will be visible in Windows Start Menu and Installed Apps. It even creates a shortcut, and your users will never have to install python themselves, or even know that your app is created with Python.
Appendix
Some explanations on your the problems you were facing.
A1) About launching a python script
When you launch a python script, you typically type python myscript.py
. What happens is that your OS (Windows) will search through a list of folders for python.exe
and when it finds it, it uses that python.exe
to run your script. This list of folders is called the PATH environmental variable.
What the activate script (Activate.ps1
for powershell and activate.bat
for cmd) does is it adds the folder of the virtual environment that contains the python.exe
to the top of the path, so when you run
python myscript.py
The python.exe
in your virtual environment (here pyenv/Scripts/python.exe
) will be used, which guarantees also that the packages installed in your virtual environment are found.
A2) Running just myscript.py
On Windows, the standard Python Installer associates .py files with Python executable. I tested it and it seems to be the special Python Launcher for Windows (py.exe
) that is used. So, when you just type
myscript.py
on command line, it will be run as <full-path-to-py>/py.exe myscript.py
. Exactly the same thing happens if you just double-click the myscript.py
in File Explorer. You can change the default program by right clicking a .py-file -> "Open With" -> "Choose application" -> Check "Always use this application", if you wish, but this does not help you with your problem.