How to source additional environment in pycharm? - python

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:

Related

Visual Code how can I activate the virtual environment automatically?

Maybe a simple question, but I cannot figure it out. I work within Visual Studio Code on a MacBook. Within a directory I create a virtual environment and have activated that with source .venv/bin/activate scripts and everything works fine.
In the terminal I see (.venv) (base) gaston#MacBook-Pro WebScraping %
When I start visual code another time and open this same directory I do not see the (.venv) so I again give manually the command to activate.
My question is there a way to activate the virtual environment automatically when opening the directory?
VS Code Default Python Interpreter Path
You can do this in a number of ways. If your goal is to have the virtual environment selected immediately at the launch of VS Code, you will have to target that virtual environment as the Python Interpreter in VSC.
https://code.visualstudio.com/docs/python/environments
System Preferences->Settings->Python: Default Interpreter Path
You'll want to point it to your virtualenv Python installation, likely something like:
envFolder/bin/python3
VS Code Launch Configuration Python Interpreter
You can also set a specific launch configuration so that VSCode will still use your system Python instance by default; however, specific launch configurations will have the virtual environment specified.
https://code.visualstudio.com/docs/python/debugging
I did something within settings and now it works.
When I open VSC now it opens the directory and the right environment, just the way I wanted it.
Took me some while to figure what finally did the trick:
I opened settings Command + , typed env in the search box. Scrolled through the results until I saw:
Python > Terminal: Activate Env In Current Terminal
and checked the checkbox. That solved my problem.

Is there a way to automatically load environment variables when activating venv?

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

how do I get python to recognize modules installed in virtual environment?

I'm having trouble getting python in vs code to work properly. There were problems for a while (i dove right in with a workspace folder) so I decided to set up everything from scratch by starting a new project/workspace folder.
so far I've been having a few issues. The latest is this: despite creative a virtual environment as instructed on the vs code website, I can't get python to recognize that I've already installed the modules I want to use.
How do I fix this? (please see attached picture.
towards the bottom you can see that the virtual environment already has matplotlib, but python wouldn't recognize the module anyway
Create your virtual environment (e.g I used venv module to create a virtual env called TEST : $python3 -m venv TEST
From VS code go to Command Palette and type Python: Select Interpreter
And then you can choose the location of the desired interpreter (in my case the one from TEST virtual environment
You can always test your virtual environment from the terminal, e.g TEST env
Bash
From Cmd/Powershell normally you should run:
TEST\Scripts\activate.bat

Can PyCharm have multiple conda environment variables for the same project

There are many discussions on how to use conda environment variables in PyCharm projects. These discussions, however, often assume that for each PyCharm project only one conda environment variable is used. Then here is my question: is it possible to have multiple conda enviroment variables for the same project? I give the following example to make my point much clearer:
pycharm_project_name
abc.py (use conda environment variable: TensorFlow)
def.py (use conda environment variable: Caffe)
In this example, the PyCharm project has two python scripts with each depending on its own environment variable. In this case, how could I set the PyCharm project in a proper way?
If your goal is to run the scripts within a particular environment, this is easily acomplished using different run configurations for the different scripts. This will allow you to select the configuration from the dropdown and click play to run it.
The run configuration dialog has a "Python interpreter" option, where you can select the environment to run in.
Unfortunately, there is no simple way to configure different environments for the other popular way of running scripts, which is to right click on them and select the run option from there.

In Eclipse I can't get environment variable

Ubunto 14.04
I'd like to keep secret info (passwords etc.) in environment variables of Ubuntu. I placed them in user's variables.
/home/michael/.bashrc
export SECRET_KEY=nh9_j12rx4j_zqiw235_klvm183p5g8bz_s2_fl3auc # Django
In terminal I check:
michael#michael:~$ echo $SECRET_KEY
nh9_j12rx4j_zqiw235_klvm183p5g8bz_s2_fl3auc
In python run under michael:
>>> os.environ['SECRET_KEY']
'nh9_j12rx4j_zqiw235_klvm183p5g8bz_s2_fl3auc'
Now I run Eclipse IDE and try to get os.environ['SECRET_KEY']. Well, no such environment variable. I think, maybe Eclipse can't access michael's variables even if michael started Eclipse.
Could you help me cope with this problem?
Since eclipse does not read your .bashrc, I guess you should specify your environment variables in the context menu where you can set the default interpreter.
Go to Run -> Run Configurations... and Select tab "Environment".
Otherwise, if you want those environment variables available in Eclipse you need to put them in /etc/environment.
You may consider using profile file or bash_profile to store the environment variables. You can refer more here
Another approach would be to start Eclipse from an open terminal window where you can access the respective environment variable and Eclipse should see them.

Categories

Resources