I have a few problems regarding the use of .bash_profile and Pycharm. I am using mac OS X. I created a new project on pycharm with new environment using virtualenv with base interpreter /usr/local/bin/python3.5.
STEP 1:
I then accessed the .bash_profile from my mac OS terminal and exported 2 variables: DB_USER and DB_PASS as my_db_user and my_db_pass respectively.
STEP 2:
Using Pycharm, I imported os and then proceeded to print out the 2 variables using os.environ.get(). Running the .py file using pycharm (F10) returns my_db_user and my_db_pass.
As I decided to create 2 new variables test user and test pass in the virtual environment, I proceeded to activate my venv (venv/bin/activate) in the shell of pycharm. Then, I removed the changes I did in STEP 1.
However, running the .py using pycharm (F10) STILL returns my_db_user and my_db_pass instead of test user and test pass (I have already removed my_db_user and my_db_pass so I have no idea where it is coming from!). On top of that, when I run the python file on the shell using python test.py, it returns (None, None) instead of my desired test user and test pass.
I need help to sort this out so that os.environ.get() returns my desired output. One possible reason is that I might be confused on how pycharm, shell in pycharm and terminals are interacting. Please help thanks!
import os
user = os.environ.get('DB_USER')
password = os.environ.get('DB_PASS')
print(user,password)
If I read your post correctly, you have already cleared out the .bash_profile file, so what remains is just running
unset DB_USER DB_PASS
in each shell window that still has the environment variables.
Related
I have the following code snippet
import time
print("Printed immediately.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")
In the VS Code: Conda Environment it does not print statement 01 first and statement 02 after 2.4s delay. Instead both are printed at the same time after 2.4s delay.This issue is not visible in the native python version (Python 3.x with Mac OS). How to resolve this issue?
p.s I found out that this error is related with the executing command. The default command which is executed is
conda run -n env_name python "path/test_print.py"
if it is changed as follows it prints with the time delay
python "path/test_print.py"
how to change the default executing command in VS Code?
I think the underlying issue here is that "conda run" buffers stdout until the command completes - see: https://github.com/conda/conda/issues/9412 - but in the mean time I guess the only workaround is to change the executing command. I think the default launch command changed in the most recent (2020.1.57204) VS Code python extension release as I didn't have any such problems before this.
I found that changing the VSCode Python extension setting for condaPath from the default (blank) to something invalid - eg condaX - has the effect of changing the launch command from "conda run -n ...", to "& C:/ProgramData/Anaconda3/envs/tf-gpu/python.exe full_path_to_py_file.py" - which has the effect of running in the appropriate conda environment (tf-gpu in my case) without the IO buffering issue. (I'm running on Windows obviously.)
I had previously set up conda launching using https://medium.com/#udiyosovzon/how-to-activate-conda-environment-in-vs-code-ce599497f20d and I'm not sure if any of those changes had any bearing on the issue but I have since reinstalled the VSCode python extension and can find no trace of those changes, so I doubt any of them are necessary for the condaPath change to work as Ive described.
I'm assuming you don't use any virtual environment in native python, you can do it by:
Open up a terminal in VS Code (or use OS' terminal)
Assuming your default python is linked to python2, type which python3. You will get something like /usr/bin/python3
Open VS Code's user settings in the project folder you in. If there is not, create one: .vscode/settings.json.
Type and save
{
"python.pythonPath": "/usr/bin/python3"
}
Or if you want to change it globally, do it in the settings file -> preferences -> settings and search for python path, and make it /usr/bin/python3
I'm using MacOS and working on a python project.
In my project there is a line:
num_workers = int(os.environ.get('NUM_SAS_WORKERS', 1))
I want to add NUM_SAS_WORKERS in my environment variables and set it to 10 so that my python project can load the value 10 into num_workers
I've tried executing this in my terminal:
export NUM_SAS_WORKERS=10
and I was able to verify that NUM_SAS_WORKERS = 10 exists by running printenv in terminal
But it doesn't work. In my python script num_workers is still loaded with 1
How to do it correctly?
If you want to access the NUM_SAS_WORKERS environment variable in the Python shell, run this command from your terminal:
NUM_SAS_WORKERS=10 python
And once in the Python shell:
>>> import os
>>> int(os.environ.get('NUM_SAS_WORKERS', 1))
10
If you want to access it in a file, very similar:
NUM_SAS_WORKERS=10 python yourfile.py
If you set up environment variables from the terminal command it gets erased as soon as you close that terminal. To set up and keep them you have to update them in .bash_profile file which resides in the home directory as a hidden file. Follow the below commands to update it.
Open terminal
cd ~
(To take you to the home directory of the mac)
open -a "Visual Studio Code" .bash_profile
( To open .bash_profile file, in the place of "Visual Studio Code" you can use any text editor name)
Now, .bash_profile will be opened and there you can set your environment variables and save it.
Example:
NUM_SAS_WORKERS=10
Now run echo $[NUM_SAS_WORKERS] in terminal to read that
After performing the above steps some times still environment variables not available to read in your python project/file. In such cases, you have to restart or log off and log in to your machine that can fix the issue.
To check whether the environment variable is available to your python code run the below script in your python console. This should print all your environment variables.
import os
print(os.eviron)
I added a environmental variable manually as setx NEWVAR SOMETHING,
so that my tool later uses the NEWVAR variable in the script but I am unable to access it, please help. Also below is the code. And for your information I am able to access the predefined system variables
try:
kiran=os.environ["NEWVAR"]
print kiran
except KeyError:
print "Please set the environment variable NEWVAR"
sys.exit(1)
You need to make sure your environment variable is persistent following a restart of your shell otherwise your new environment variable will not be accessible later on
ekavala#elx75030xhv:/var/tmp$ export NEWVAR='alan'
ekavala#elx75030xhv:/var/tmp$ python test.py
alan
*closes shell and reopens*
ekavala#elx75030xhv:/var/tmp$ python test.py
Please set the environment variable NEWVAR
Update your $HOME/.bashrc or /etc/environment with the variable instead of just doing a setx or export
Note: If you update /etc/environment you will need to reboot your computer to have the environment variables set in your shell
If you want to set environment variable through script:
import os
os.environ["JAVA_HOME"] = "somepath"
If you set it using command prompt it will be available only for that shell. If you set it in advanced system settings it will available every where but not when you open a new shell inside python script.
Instead of using setx to set those environment variables; try using EXPORT. The following example worked for me.
export MYCUSTOMVAR="testing123"
python testing.py
Note: If you are on Windows you can set the env variable with SET.
SET MYCUSTOMVAR=testing123
testing.py
import os
if __name__ == '__main__':
print("MYCUSTOMVAR: {0}".format(os.environ['MYCUSTOMVAR']))
output
MYCUSTOMVAR: testing123
trying to install spark, I've some problems when I try to set the system enviroment variables. I modify the PATH using:
“Advanced system settings” → “Environment Variables”
but when I call these variables from python, using the code:
import os
path = os.environ.get('PATH', None)
print(path)
The path that shows python don't have the modifications that I put. Thanks
Any program invoked from the command prompt will be given the environment variables that was at the time the command prompt was invoked.
Therefore, when you modify or add an environment variable you should restart the command prompt (cmd.exe) and then invoke python to see the changes.
I have a python script file that works perfectly when I use it from the terminal.
Now I have created the following .desktop file in order to launch it easily:
[Desktop Entry]
Name=Test
GenericName=Test
Comment=My test script
Type=Application
Exec=/opt/test.py
Icon=/opt/test.png
Categories=Utils;
When I launch it the GTK window appear but clicking a button that call an init.d script make it working not properly.
Therefore adding Terminal=true make it working perfectly but I don't want to have that terminal open.
So I have then put the following code in order to log the environment variables:
import os
with open("/tmp/py_env.log", "w") as env_log:
env_log.write(str(os.environ))
and found differences.
So my question is how to write the .desktop file so that my application is running like if I start it from my terminal (without having an opened terminal :))
The problem is valid, but I think "replicating the terminal environment" is the wrong approach to solve it.
Indeed, what makes the application work is not the fact that it's launched from the terminal, it's that the terminal happens to have some environment variables which matter to your application.
Therefore, what you should aim for is to have those environment variables set properly at all times, rather than assuming the terminal environment will always happen to contain them all the time for all your users.
Thus, you should:
Check which environment variables are different between the two environments
Make a list of those which matter (i.e. those which would make the .desktop file work properly), and of what their value needs to be for the script to work
Either:
Create a wrapper script for your Python script, which initializes those environment variables properly, OR
Set those environment variables from inside the Python script itself.
this question is similar to .bashrc not read when shell script is invoked from desktop shortcut
either initialize your environment in ~/.bash_profile instead of
~/.bashrc
OR
make your *.desktop file call a wrapper that initializes your
environment - e.g. by sourcing ~/.bashrc (or whatever script is
responsible now).
the second solution is more specific (does not effect all other unrelated launches of your shell) in should thus be preferred.
Thanks anyone to have participate to this question.
I have solved this issue by implemented use of pkexec instead of gksudo.
pkexec seems to reuse the current user environment then I don't have this issue anymore.
Thanks.