I am using Delphi 10.4 to invoke a python script using the Py4Delphi library.
How can I execute the script with respect to a particular python virtual environment directory?
Using the virtual environment is basically setting a few environment variables before running python. You can look into scripts\activate.bat which variables that are.
You don't need to recover those changes after running it, because within a process you're running with a copy of the system environment.
Related
I am using python venv to create virtual environments. But, since I am working with several projects with different virtual environments, I don't want to manualy set environment variables every time I switch to a different project.
Is there a way to set venv environment variables automatically when activating the venv?
What is the best practice for this problem?
A good practice is to use dotenv. You can load your environment by placing your environment variables into a file named .env, and whenever you would like to load an environment, just use the lines:
from dotenv import load_dotenv
load_dotenv()
This has the nicety that it only exists within the scope of you running a single script, since it essentially works like calling os.environ['variable'] = 'value' a number of times.
Activating a virtual environment is nothing more than sourcing a shell script. You can edit that script to set whatever variables you like. You will probably also want to edit the definition of deactivate to clear or roll back whatever changes you made to the environment.
you need to write a bash scirpt (in case you are using bash shell), where you specified a particular command which will activate the project python environment and add the project specific envrionment variable in the system environment. and remove the environment variable when you exit the project python environment.
but i don't this is good/correct way to do things. #mz solution will be correct, where you define a .env file and define env variable in it. and use load_env to read the env variable when project runs
This concept is based on Two Scoops of Django. I have implemented it using venv.
Open the Windows PowerShell Script in your virtual environment generated by venv.
The script is located at venv/Scripts/Activate.ps1
At the bottom of the file, you will see this line of code:
$env:VIRTUAL_ENV = $VenvDir
Below that code, enter your environment variable as follow:
$env:VARIABLE_NAME = 'variable_value'
Same concept goes if you are using the Command Prompt to activate the environment, you will need to place environment variables in venv/Scripts/activate.bat
I have a ROS application which has a work space with a setup.bash file and another python script with its own virtual environment.
So far this is what I do in my terminal:
1_ pipenv shell (to activate my python virtual environment).
2_ source ../ros_workspace/devel/setup.bash
3_ python some_python_script.py
This code works as I expect.
However, I want to do the same and run this script in pycharm, where my virtual environment is already activated. But how do I source the setup bash additionaly?
My setup.bash file also looks like the following:
What I have tried also is making a "before launch" as follows:
If you set your virtual environment as your interpreter of choice in PyCharm, it will use that particular virtual environment to run its scripts. However, you can also take advantage of some of the functionality that our run configurations provide.
You can check out the "Before Launch" part of the whole configuration window to enter scripts that you want executed.
Once you've set your configurations, you can then go on to run or debug the configuration. Furthermore, if it is just environment variables that you want to source, you can just put in the environment variables in the "Environment Variables" box.
In case you want to run a shellscript, you will need to create a new shell configuration like so:
Once you've added that configuration, you can then go on to reference it later.
You will now see that you can reference that configuration in question:
I have a python file that I'd like to execute using the virtual environment (venv) in the same directory as the file. I'd like it to use this venv regardless of whether the user has activated it or not to avoid accidentally running it without the proper environment. I'd like this to work on both Linux and Windows (via Git Bash).
The issue is that venv puts python under the "bin" directory on Linux but under "Scripts" on Windows and I can't seem to find a way to change this behavior when creating it. I tried creating it with Git Bash in hopes that it would "trick" python into using a bin directory instead of Scripts, but that didn't work.
The following shebang works well on Linux:
#!.venv3/bin/python
And this one works well on Windows:
#!.venv3/Scripts/python
But what will work on both? I know that one option would be to create a shell script that activates the environment based on the detected os (using $OSTYPE), but I'd like to avoid this if possible as it isn't otherwise necessary.
You could use the Windows version of the shebang:
#!.venv3/Scripts/python
and then create a symbolic link on the Linux side:
.venv3/Scripts -> .venv3/bin
I just tried this with one of my virtual envs. It worked fine for me. I didn't test the Windows case, but of course, that has to work as we're using the correct shebang for Windows to begin with.
I have to develop software with the use of several programs together, like Python 3.6 (with specific packages), WKHtmlToPdf and an embedded browser (based on CEF). I should be able to share all this together in one directory, so with a virutal environment of Python 3.6...
My question to you all is: how can I make the paths of a Python virtual env relative.
A second question I have is: is it possible to just activate the virtual env without changing cmd prompts and things? I tried to write a batch file which 1) starts the environment, 2) executes a Py script and 3) runs the embedded browser. That batch file failed after it started the virtual environment...
I should be able to share all this together in one directory, so with a virtual environment
Virtual environments are not portable (they must reside in the same directory on a computer where exist global Python), hence you cannot share a virtual environment.
My question to you all is: how can I make the paths of a Python virtual env relative.
No, and you certainly don't need to do it.
A second question I have is: is it possible to just activate the virtual env without changing cmd prompts
Yes, it's regulated in bin/activate script which you can edit to your taste.
and things?
What "things"? Activating virtual environment means changing PATH, PYTHONPATH and so on, you certainly cannot use virtual environment without changing these and other things.
That batch file failed
If you want us to help you should show us the essential parts of the batch and in what way it failed.
May be you need a different solution like a Docker image. Docker can be installed on Windows though it must be installed on the target computers.
There are also simpler solutions: py2exe, PyInstaller, cx_Freeze. They "compile" python scripts to .exe and accompany that .exe with Python interpreter (in a .dll) and library (in a .zip file). But if your program needs more DLLs you have to copy them to the target computer.
You can also take a look at RVirtualEnv. It allows to create relocatable virtual environments. I don't know if such envs can be copied to a different host, though.
We have an in-house developed network-based application that is based on python, it provides a command-line environment in the same way like python does. I also have a local python installation on my machine. Sometimes for simple python tasks, i prefer using my local python install... Is possible to to have a different PYSTARTUP env variable for each of installation?
You can always export PYSTARTUP="whatever" on the shell before starting your script.
You can also put PYSTARTUP="whatever" in front of the command you want to run, e.g. PYSTARTUP="whatever" python something.py