PyCharm - automatically set environment variables - virtualenv

I'm using virtualenv, virtualenvwrapper and PyCharm.
I have a postactivate script that runs an "export" command to apply the environment variables needed for each project, so when I run "workon X", the variables are ready for me.
However, when working with PyCharm I can't seem to get it to use those variables by running the postactivate file (in the "before launch" setting). I have to manually enter each environment variable in the Run/Debug configuration window.
Is there any way to automatically set environment variables within PyCharm? Or do I have to do this manually for every new project and variable change?

I was looking for a way to do this today and stumbled across another variation of the same question (linked below) and left my solution there although it seems to be useful for this question as well. They're handling loading the environment variables in the code itself.
Given that this is mainly a problem while in development, I prefer this approach:
Open a terminal
Assuming virtualenvwrapper is being used, activate the virtualenv of the project which will cause the hooks to run and set the environment variables (assuming you're setting them in, say, the postactivate hook)
Launch PyCharm from this command line.
Pycharm will then have access to the environment variables. Likely because of something having to do with the PyCharm process being a child of the shell.
https://stackoverflow.com/a/30374246/4924748

I have same problem.
Trying to maintain environment variables through UI is a tedious job.
It seems pycharm only load env variables through bash_profile once when it startup.
After that, any export or trying to run a before job to change bash_profile is useless
wondering when will pycharm team improve this
In my case, my workaround for remote interpreter works better than local,
since I can modify /etc/environment and reboot the vm
for local interpreter, the best solution I can do are these:
1. Create a template Run/Debug config template and clone it
If your env variables are stable, this is a simple solution for creating diff config with same env variables without re-typing them.
create the template config, enter the env variables you need.
clone them
see picture
2. Change your script
Maybe add some code by using os.environ[] = value at your main script
but I don't want to do this, it change my product code and might be accidentally committed
Hope someone could give better answer, I've been spent too much time on this issue...

Another hack solution, but a straightforward one that, for my purposes, suffices. Note that while this is particular to Ubuntu (and presumably Mint) linux, there might be something of use for Mac as well.
What I do is add a line to the launch script (pycharm.sh) that sources the needed environment variables (in my case I was running into problems w/ cx_Oracle in Pycharm that weren't otherwise affecting scripts run at command line). If you keep environment variables in a file called, for example, .env_local that's in your home directory, you can add the following line to pycharm.sh:
. $HOME/.env_local
Two important things to note here with respect to why I specifically use '.' (rather than 'source') and why I use '$HOME' rather than '~', which in bash are effectively interchangeable. 1) I noticed that pycharm.sh uses the #!/bin/sh, and I realized that in Ubuntu, sh now points to dash (rather than bash). 2) dash, as it turns out, doesn't have the source "builtin", nor will ~ resolve to your home dir.
I also realize that every time I upgrade PyCharm, I'll have to modify the pycharm.sh file, so this isn't ideal. Still beats having to manage the run configurations! Hope it helps.

OK, I found better workaround!
1.install fabric in your virtualenv
go to terminal and
1. workon your virtualenv name
2. pip install fabric
2. add fabric.py
add a python file and named it "fabric.py" under your project root, past the code below,and change the path variables to your own
from fabric.api import *
import os
path_to_your_export_script = '/Users/freddyTan/workspace/test.sh'
# here is where you put your virtualenvwrapper environment export script
# could be .bash_profile or .bashrc depend on how you setup your vertualenvwrapper
path_to_your_bash_file = '/Users/freddyTan/.bash_profile'
def run_python(py_path, virtualenv_path):
# get virtualenv folder, parent of bin
virtualenv_path = os.path.dirname(virtualenv_path)
# get virtualenv name
virtualenv_name = os.path.basename(virtualenv_path)
with hide('running'), settings(warn_only=True):
with prefix('source %s' % path_to_your_export_script):
with prefix('source %s' % path_to_your_bash_file):
with prefix('workon %s' % virtualenv_name):
local('python %s' % py_path)
3. add a external tool
go to
preference-> External tools -> click add button
and fill in following info
Name: whatever
Group: whatever
Program: "path to your virtualenv, should be under '$HOME/.virtualenvs' by default"/bin/fab
Parameter: run_python:py_path=$FilePath$,virtualenv_path=$PyInterpreterDirectory$
Working directory: $ProjectFileDir$
screenshot
wolla, run it
go to your main.py, right click, find the external name (ex. "whatever"), and click it
you could also add shortcut for this external tool
screenshot
drawbacks
this only work on python 2.x, because fabric don't support python 3

Related

Add existing make environment to eclipse

I have a question about eclipse and make. I have created a make environment which acts as follow:
I have a batch file (Start.bat) which SET´s variables for further usage (e.g. SET TOOL_PATH = blabla).
After calling this I use my make.bat <option>, which calls the make.ext -f Makefile.mak <option> with the given option e.g. make.bat all -sj4. The environment variables which are set in Start.bat are very important for the running of the overall make environment.
This works very fine and now I would like to import this in eclipse but I am not familiar with eclipse. I need from eclipse that when I would like to run a make, first the Start.bat is called and then the make.bat <option> is called.
What I need to do?
You can import your project as 'existing make file project'
In the project settings you should be able to define pre-biuld steps and call the batch there or define a specific target (e.g. start) that calls the start batch and is executed before your 'make'.
I dont know exactly at the moment how to do this the best way, but I think you are fast into it.

Where should I place my settings and profiles for use with IPython/Jupyter 4.0?

I've recently updated IPython (to 4.0) and have started using Notebooks after a period of some time not using them (since before The Big Split, I think), and have discovered that some of my settings need to be modified and moved from ~/.ipython/ to ~/.jupyter/.
For example, it appears that the NotebookManager.notebook_dir in
~/.ipython/profile_default/ipython_notebook_config.py
is ignored and has had its functionality replaced by FileContentsManager.root_dir in
~/.jupyter/jupyter_notebook_config.py
Beyond isolated incidents like this, however, it's not clear to me how the configuration settings and profiles now work. In particular what files should be located where, and what they should contain. If, for example, I generate a new profile with
ipython profile create foo
I now get different files than I used to (and than those present in my existing profiles): where I used to get
ipython_config
ipython_notebook_config.py
ipython_nbconvert_config.py
I now get
ipython_config.py
ipython_kernel_config.py
and as near as I can tell, none of my ipython_notebook_config.py are used.
This (and the limited documentation) all leaves me a bit confused about where should I place my settings and profiles for use with IPython/Jupyter 4.0, specifically:
Where should I place general notebook-related settings for use by all kernels/profiles? (I see that some go in ~/.jupyter/jupyter_notebook_config.py, but it's not clear to me whether these would override settings loaded from elsewhere.)
Where should I place profile-specific settings for use with notebooks? (Can I continue to use ipython_notebook_config.py files in ~/.ipython/profile_someprofile/ directories?)
Where should I place kernel-specific settings for use by all profiles and applications. (For example, where should settings used by IPython for all profiles in both the shell and notebooks go?)
How should I modify or update existing IPython profiles for use with 4.0.0? (For example, should all have a ipython_kernel_config.py file; should I delete the ipython_notebook_config.py and ipython_nbconvert_config.py files?)
OS X 10.10.4; Xcode 6.4; CLT: 6.4.0.0.1; Clang: 6.1; Python Python 2.7.10 (Homebrew); Jupyter 4.0.4; IPython 4.0.0.
First of all, check what's in your ~/.jupyter/ folder. Some of the comments under the question mention you have a file named "MIGRATED" that contains only a timestamp.
If you are in this state, run the command: jupyter notebook --generate-config which will create a new file ~/.jupyter/jupyter_notebook_config.py. If you already have this file, you don't need to run this command.
This file is where you put the code that will modify your IPython Notebook behavior. It's a fully commented out (and well documented) file.
I have a related question open (How do I get IPython profile behavior from Jupyter 4.x?) that deals with attempting to set up multiple profiles, which is currently unanswered.

Set Environment Variable At Eclipse workspace level for all launchers?

Can I set a workspace wide setting to add an environment variable to all future launchers created in the workspace?
Use Case
Our unit tests require an environment variable to guide the test to certain resources.
the variable varies with each version of our product
Options
- modify each junit launcher with the environment variable
- create start up script that sets variable and launches eclipse
- set globally
Ideally, I'd like to provide a way for users to set it once per workspace.
Does eclipse have a place to set an environment variable for all launchers?
Thanks
Peter
Only option which I can think of is similar to yours where you can create multiple shell scripts(linux) or batch files(windows) and set it up there. for ex -
I am giving examples for windows environment and same can be done for Linux as well -
for windows-
eclipse.exe -DvariableName1=value1 -DvariableName2=value2
As you want to pass different variables to different workspaces, you can also pass location of your workspace as part of arguments.
eclipse.exe -data <your_workspace_location> -DvariableName1=value1 -DvariableName2=value2
You can create multiple shortcuts of eclipse.exe in windows and place them on Desktop (for quick access) if needed. Each shortcut may point to a similar variant of above command with different workspace and different variables.
Hope this helps. Happy coding :)
I have a different approach for doing this on windows. This solution allows you to store a large number of environment variables to one place and apply them globally inside eclipse.
I have gitBash installed already, and associated in windows to execute .bash files
I set up a /c/Temp/.env file with lots of environment variables in the format
export VARIABLE=my value
I create an eclipse_startup.bash script that looks like this (and pin it to my start menu).
echo setting up env
. /c/Temp/.env
echo starting eclipse
/c/Users/me/eclipse-oxygen/eclipse/eclipse.exe
It has the disadvantage that I end up with a bash window open with eclipse, and I have to restart after changing the environment. On the plus side, my complex application has lengthy environment files already, so I can simply load them in and start.

run pydev project from file-system (with imports from different packages)

I want to run my working pydev project python code by double clicking the main module (outside of eclipse): xxx.py
The problem is that due to my imports being in different packages:
from src.apackage.amodule import obj
when xxx.py is double clicked it complains it doesn't know where the imports are (even though when I run xxx.py in pydev it magically knows what I'm importing).
A simple workaround is to remove all of the packages and move all of the modules into one directory (that obviously works but is very inconvenient)
How can I run my code in the file system without doing that work around?
This page answers my question excellently:
http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/
Bottom line is always execute your code from the top, highest level, root directory (e.g. using a minimal main.py file that executes the main script of your program). Then, use absolute imports always and you never have a missing module issue since you start the program from the top directory and all imports are based off that 'home' path.
The problem you encountered is the natural behavior of most languages. A programm only knows about its working path (the path it is started in), the paths which are registered in the environment variables and at least relative paths.
The "magic" of the executable you created is therefore: It collects all scripts/modules needed, and copies/combines them next to/in the executable. The Executable then runs within the directory where all other scripts also reside and voila ...
If you are not happy with your workaround of creating an executable every time you want to run your project without PyDev there are two alternatives.
First but not the one I would suggest is registering the working path into in the environment variables.
Second and the one I think is much better: Create a link to the python executable and alter the calling string of the textfield "Target:". Append the path to your script you would like to run. Then alter the textfield "Start in:" and enter the project directory. After you did this you should be able to start your project with a simple double click.
(If you rely on external libraries which are neither on the path nor in you project you could search for appending paths temporarily to the pythonpath via the sys module.)
I hope I could help a bit.

How do I configure Eclipse's Subversion config file to use my private-key?

I'm using Eclipse and the Subversive for SVN control, in Windows. I've also installed TortoiseSVN. The SVN repository I'm using is utilizing a svn+ssh scheme, and there is no way to change that.
Now, there's a file in %APPDATA%\Subversion\config that I need to edit to perform SVN interaction in Eclipse without typing in the password every time. The line in this config file reads now (default):
ssh = $SVN_SSH ssh -o ControlMaster=no
I've read a bit on the web that the $SVN_SSH is an environmental variable in Windows, and in this case it points to TortoiseSVN\bin\TortoisePlink.exe, which is correct.
What is the additional parameteres to append this config to make it take my private key? I've updated the "authorized_keys" file on the server (in my .ssh folder) with my public keyphrase, and I've tested and confirmed that logging in to the SVN server outside Eclipse works like it should with my private/public key pair.
The problem here is to make it all work inside Eclipse, and it's quite frankly a small nightmare :/ If anyone has experienced the same, or knows any remedy, I'd appreciate any help.
If you set the 'SVN_SSH' environment variable the rest of the line is ignored, so you should either replace '$SVN_SSH ssh' with the path to the program, or move the required arguments to the end of the SVN_SSH environment variable.