CDK Python: conflicts with node version manager (nvm)

0

I'm using CDK with my preferred language as Python. CDK is a node app, so has to be run in an environment with node available.

I make node available in my environment using the Node Version Manager, which works by sourcing my ~/.bashrc (I'm running Ubuntu).

It seems that activating a python virtual environment has the effect of "turning off" the node environment that is activated by my .bashrc when I first start up my shell.

So for instance, if I start a shell, cd into an empty directory and do...

cdk init app --language python

cdk puts all the required boilerplate in that directory, indicating node is working.

But then, when I do

source .venv/bin/activate

node is no longer available. For instance, if I then do

cdk list

My shell says 'command not found'.

Now the way Node Version Manager activates node via ~/bashrc. So you might think that the way to fix this problem is to run source ~/.bashrc after activating the virtualenv. But this has the effect of de-activating the virtual env!

So the question...how can I make NVM and virtualenv play well together so that I can use CDK with python as the preferred language.

Thanks!

asked a year ago204 views
1 Answer
1
Accepted Answer

It sounds like activating the Python virtual environment is overriding the Node version set by NVM. A few things you could try:

  • Add the NVM init script to the activate script of your Python virtualenv. This will ensure NVM is run everytime you activate the virtualenv:
echo "source ~/.nvm/nvm.sh" >> .venv/bin/activate
  • Set the NODE_VERSION environment variable before activating the virtualenv. This will force a specific Node version when the virtualenv is activated:
export NODE_VERSION=v10.15.3
source .venv/bin/activate
  • Use a tool like n to switch Node versions instead of NVM. n can set Node version globally so it persists in new shells/environments.

  • Use a Docker container or similar to encapsulate the environment with both Node and Python configured how you need.

The core issue is that virtualenv overrides the shell environment, so you need to find a way to re-initialize NVM after activating virtualenv. Hopefully one of those suggestions helps! Let me know if you have any other questions.

AWS
answered a year ago
EXPERT
reviewed a year ago
EXPERT
reviewed a year ago
  • Thank you! All that sounds good. I ended up fixing things simply by running nvm use default in my shell immediately after doing source .venv/bin/activate.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions