1

I'm using virtualenvwrapper to manage a project where I'm just running this for now

import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions

with beam.Pipeline(options=PipelineOptions()) as p:
    pass

This is returning the following error

Traceback (most recent call last):
  File "path/to/pipeline.py", line 1, in <module>
    import apache_beam as beam
ModuleNotFoundError: No module named 'apache_beam'

I have run pip install apache-beam. Running pip list returns

➜ pip list  
Package                        Version
------------------------------ ---------
apache-beam                    2.23.0
avro-python3                   1.9.2.1
...

I'll add the following outputs as suggested in a similar question.

pip freeze

➜ pip freeze
apache-beam==2.23.0
avro-python3==1.9.2.1
...

pip -V

➜ pip -V    
pip 20.2.2 from /Users/miguel/.virtualenvs/myenv/lib/python3.7/site-packages/pip (python 3.7)

python -V

➜ python -V        
Python 3.7.3

which python

➜ which python
/Users/miguel/.virtualenvs/myenv/bin/python

which pip

➜ which pip   
/Users/miguel/.virtualenvs/myenv/bin/pip

I don't know if this is relevant but I'm using VSCode and I have selected my python interpreter according to VSCode instructions here. Additionally, I installed python following the instructions here.

Any idea why this is happening?

mcansado
  • 2,026
  • 4
  • 25
  • 39
  • Have you tried to test the environment set up outside of VSCode? For example, opening the terminal/command prompt, activating your virtual environment and then running Python in the terminal with this venv. Then once Python is running in the terminal you can try your import with apache_beam. I think this will help narrow down if it's a module install/dependency problem or if your venv isn't running as expected in VSCode. – Robert Young Aug 20 '20 at 08:37
  • 1
    Thanks! I just tried this and it does work so it's probably a VSCode issue in that case. I don't understand because it's running the same interpreter like I mentioned above... – mcansado Aug 20 '20 at 10:00
  • 1
    Great, so it looks like a VSCode env set up, I'll put together an answer with the above issue testing and some tips on how to set up your Python Env in VSCode – Robert Young Aug 20 '20 at 10:02
  • I've posted an answer, let me know if it works. If not I will recreate the steps on my machine to recreate and solve the issue and post further info in the answer. – Robert Young Aug 20 '20 at 10:14

1 Answers1

0

First test if your installed Python modules work outside of an ide like VSCode. We can do this by opening the terminal/command prompt and activating our virtual enviornment. In this case, as you're using virtualenvwrapper you need to use the command:

workon myenv

Documentation for the virtualenvwrapper can be found here. Once activated, we can open the Python interpreter in our terminal by using the command:

python

Once running, try and import apache_beam as before using:

import apache_beam as beam

If this works we now know it's a virtual environment set up issue using VSCode. For setting up Virtual Environments for Python in VSCode, use the official documentation Using Python environments in VS Code. This should allow you to specify the virtual environment.

If this fails to work, another option is to create a new virtual environment in VSCode and install your modules. Installing modules to a environment in VSCode can be found in the VSCode instructions you linked to in your post.

I tried to re-create your issue as above using the following:

  • Python 3.7.7
  • VSCode 1.48.0
  • apache-beam 2.23.0

My virtual environment was named 'stack'. Once I had created it the only module I installed was apache-beam using:

pip install apache-beam

When creating the project I made the directory and launched via terminal using:

 - mkdir hello
 - cd hello
 - code .

And then added the Python interpreter via the command:

Python: Select Interpreter

One additional step I needed (as I usually use PyCharm for Python) was to install:

Shell Command: Install 'code' command in PATH** command.

To allow the launch of code via terminal. Below is a screenshot of the project after following these steps and running the same commands as posted in your question for reference.

enter image description here

My .vscode/settings.json is as follows:

{
    "python.pythonPath": "/Users/robertyoung/envs/stack/bin/python"
}
Robert Young
  • 456
  • 2
  • 8
  • Thanks for the answer! The initial part does work so I agree it seems like a virtual environment set up issue using VSCode. However, I don't know what that could be because (1) VSCode correctly finds my `virtualenv` and (2) I selected that `virtualenv` from "Python: Select Interpreter" as specified [here](https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment). If I press the play icon (⌃⌥N), I get the error. However, if I do "Python: Run Python File in Terminal", it does work. – mcansado Aug 20 '20 at 12:32
  • Are you running on Windows or Mac/Linux? – Robert Young Aug 20 '20 at 12:48
  • I am running Mac – mcansado Aug 20 '20 at 12:51
  • Btw only realised that it ran with "Python: Run Python File in Terminal" when I post that comment. – mcansado Aug 20 '20 at 13:18
  • I tried to recreate this problem on my Mac, set up a new virtual environment using virtualenvwrapper, new VSCode project and set the interpreter to this new env. I used pip install apache-beam and it is able to import correctly. I will add images to my answer which will show the steps I took to see if they help – Robert Young Aug 20 '20 at 13:18
  • Thanks a lot for your help! I don't know if there is any additional info you could need from me which could help detect the problem but let me know if there is. – mcansado Aug 20 '20 at 13:22
  • I don't think so, I've shown you the steps I took to install, activate venv, and get it running on my machine (Mac). The last thing you could try is relaunching VSCode and your project, if you haven't already! – Robert Young Aug 20 '20 at 13:32