#๐Ÿ”’ Where do I get activate_this.py? When I create a venv its not here

67 messages ยท Page 1 of 1 (latest)

granite burrow
#

I'm writing an installation sctipt, and in one of my previous posts an user told me that i need to use runpy.run_path(path) to activate the venv and be able to install pip packages (using the script). But when I create a venv it doesnt create activate_this.py. where do i get it?

fading iceBOT
#

@granite burrow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

gloomy briar
#

does it not have any activate script?

granite burrow
gloomy briar
#

is this Linux?

fading iceBOT
#
Virtual environments

Virtual environments are isolated Python environments, which make it easier to keep your system clean and manage dependencies. By default, when activated, only libraries and scripts installed in the virtual environment are accessible, preventing cross-project dependency conflicts, and allowing easy isolation of requirements.

To create a new virtual environment, you can use the standard library venv module: python3 -m venv .venv (replace python3 with python or py on Windows)

Then, to activate the new virtual environment:

Windows (PowerShell): .venv\Scripts\Activate.ps1
or (Command Prompt): .venv\Scripts\activate.bat
MacOS / Linux (Bash): source .venv/bin/activate

Packages can then be installed to the virtual environment using pip, as normal.

For more information, take a read of the documentation. If you run code through your editor, check its documentation on how to make it use your virtual environment. For example, see the VSCode or PyCharm docs.

Tools such as poetry and pipenv can manage the creation of virtual environments as well as project dependencies, making packaging and installing your project easier.

Note: When using PowerShell in Windows, you may need to change the execution policy first. This is only required once per user:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
gloomy briar
#

do source venv/bin/activate

granite burrow
#

will that work

granite burrow
gloomy briar
#

like a bash script?

granite burrow
#

python

gloomy briar
#

its possible

granite burrow
#

i know

#

but how

gloomy briar
#

so you're saying you wanna write your own python script, that activates the venv, then pip install the requirements?

#

its not the best way to do it but you probably need to use something like subprocess to execute host level commands

#

I wouldn't use it. I'd rather control my own installs

#

!d subprocess

fading iceBOT
#

Source code: Lib/subprocess.py

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions...

gloomy briar
granite burrow
granite burrow
gloomy briar
#

what is this thing that you want users to install?

granite burrow
#

ive asked my friend to install it and he had ...many problems with it and i had to guide him cuz he is a windows user and isnt knowledgeable with the windows terminal, hence the need for an installation script

gloomy briar
#

Personally, I wouldn't use tkinter. Trying to compile it into a binary executable file causes problems with pyinstaller or nuitka or whatever. If you insist on using Python, consider building with Flet https://flet.dev and then properly build for each platform. For windows, you compile it and sign it properly via Visual Studio https://flet.dev/docs/publish/windows

Build multi-platform apps in Python powered by Flutter.

Flet CLI provides flet build windows command that allows packaging Flet app into a Windows application.

#

But its your project tho

#

There are easier ways to distribute your app but you have to find a way to properly bundle it first before you can distribute it

granite burrow
gloomy briar
#

Thats completely fine

granite burrow
#

ive never said that i want to compile it

#

to an executeable

gloomy briar
#

I suggested to compile it so its easier for the users to install your app

#

As opposed to writing a separate custom installer script

granite burrow
#

and also im halfway done with the script

gloomy briar
#

its not tkinter that doesnt like being compiled. Its just not properly signed so it doesnt get flagged for viruses by MS Defender or whatever after its compiled

granite burrow
gloomy briar
granite burrow
granite burrow
# gloomy briar pyinstaller? nuitka? beeware? idk. Try them. I've not compiled any Python GUI in...

Well, i did this

def setup_venv(): # sets up the venv, and installs pip dependencies.
 print("\nVenv setup")
 print("[1/2] Setting up the venv")
 subprocess.run([sys.executable, "-m", "venv", "venv"], cwd=f"{default_download_dir}/btotp-main", check=True)
 print("[2/2] Installing pip dependencies")
 subprocess.run([f"{default_download_dir}/btotp-main/venv/bin/pip", "install", "-r", "requirements.txt"], cwd=f"{default_download_dir}/btotp-main", check=True)
#

And it works

gloomy briar
#

Cool

granite burrow
#

Thanks for help!

slate bough
#

think of it like running each command in a new cmd window each time

granite burrow
slate bough
#

oh, okay, now i see what you are doing ๐Ÿ‘

#

yeah, you just run the pip from the path within the venv

#

that is the proper way of doing it

granite burrow
#

after setup() its gonna execute another function which will create a .desktop file which will execute path/vev/bin/python path/src/exec.py

granite burrow
#

I cant really keep it activated

#

so i walked around it

slate bough
#

i saw talk before about activating the venv and though that you were still trying that, but now when i look at the command more carefully i see that you do it correctly

slate bough
slate bough
#

it's the same when you want to execute a python script within the venv, you just use the path to the python executable within the venv and then the path to the script

granite burrow
fading iceBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.