You are not logged in.

#1 2024-08-15 12:55:58

gaming
Member
Registered: 2024-04-14
Posts: 29

[RESOLVED] Python Env configure

I want a way to enable the automatic source of the virtual enviroment of python after it has been created by the user

Last edited by gaming (2024-11-16 08:30:03)

Offline

#2 2024-08-15 12:59:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [RESOLVED] Python Env configure

Huh?

Do you just want a shell function that combines the creation and activation steps?  If so, then add the function to your shellrc.  I'm really not clear at what level you're asking this question.  Can you give a use-case for how you would like the results of this goal to work?


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2024-08-16 05:51:11

jack476
Member
Registered: 2016-09-02
Posts: 20

Re: [RESOLVED] Python Env configure

You can just chain the commands to create the venv and activate it if you want to be able to do this as a single operation:

 python -m venv /path/to/venv && source /path/to/venv/bin/activate 

Offline

#4 2024-08-27 07:34:49

gaming
Member
Registered: 2024-04-14
Posts: 29

Re: [RESOLVED] Python Env configure

Well thank you for replies

I am asking a way or command that will automatically source a already created python venv, so we don't have to source it every time we want to use pip

Offline

#5 2024-08-27 12:29:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [RESOLVED] Python Env configure

What do you mean by "automatically"??  You say you want a command to source an existing env (that command is source), but you want to be able to source it without having to source it?  What does that even mean?

Again, what do you actually want to do?


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2024-08-27 21:20:59

lkmarch
Member
From: Poland
Registered: 2022-02-13
Posts: 3

Re: [RESOLVED] Python Env configure

Hi

If you want to activate virtualenv automatically when you enter specific directory there are few possibilities

Option A

You can install direnv https://direnv.net/

In configuration file ~/.direnvrc add

layout_virtualenv() {
  local venv_path="$1"
  source ${venv_path}/bin/activate
}

In your directory create file .envrc with content:

layout_virtualenv /path/to/your/venv

And call direnv allow

Now every time you enter this directory virtualenv will be activated.

Option B

Use pyenv to maintain python version https://github.com/pyenv/pyenv
Install pyenv-virtualenv plugin https://github.com/pyenv/pyenv-virtualenv

To create virtualenv call pyenv virtualenv <python version> <virtualenv name>.
To activate virtualenv in current directory pyenv local <virtualenv name  - this will create file .python-version containing your virtualenv name. Every time you will enter this directory you virtualenv will be activated.

Offline

#7 2024-08-27 21:27:42

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 675

Re: [RESOLVED] Python Env configure

You don't have to source the venv to use pip:

$ python -m venv .venv
$ .venv/bin/pip install whatever

uv offers a much faster and better way anyhow. This is equivalent to above:

$ uv venv
$ uv pip install whatever

Offline

#8 2024-11-01 05:50:59

gaming
Member
Registered: 2024-04-14
Posts: 29

Re: [RESOLVED] Python Env configure

I have found another way a package called pipenv

https://github.com/pypa/pipenv

Last edited by gaming (2024-11-01 05:51:17)

Offline

Board footer

Powered by FluxBB