Using GnuRadio Companion under a virtual environment - python

I have created a virtual environment containing packages I need for some python out of tree blocks. When I activate the virtual environment and attempt to run the flowgraph from companion, it complains that the special packages I included in my virtual environment cannot be found.
Interestingly, I can successfully run from the command line the .py version of the flowgraph automatically generated by GnuRadio Companion.
How can I get this to work under Gnu Radio Companion?
As a specific example, I created a virtual environment containing the pandas package and attempted to import pandas in a no_block type custom python block originally created with gr_modtool. I did the install with the virtual environment activated. While running the flowgraph from the command line works fine, I get the following error when attempting to run the same flowgraph from GRC (which was opened from the command line with the virtual environment activated):
Traceback (most recent call last):
File "/home/my_name/devel/gr-my_oot_module/examples/my_flowgraph.py", line 35, in <module>
import my_oot_module
File "/home/my_name/devel/gnuradio3_8/lib/python3.6/dist-packages/my_oot_module/__init__.py", line 39, in <module>
from .my_noblock_block import my_noblock_block
File "/home/my_name/devel/gnuradio3_8/lib/python3.6/dist-packages/my_oot_module/my_noblock_block.py", line 25, in <module>
import pandas
ModuleNotFoundError: No module named 'pandas'

You have to realize that this is standard python under the hood. If you don't start gnuradio-companion from within your activated environment, Python can't find the modules therein. That's the whole idea of virtual environments.

Related

Tensorflow import in VSCode fails

I'm having trouble to import TensorFlow in my python application, BUT only
if I'm running my application in VSCode (via the debugger) OR
if I'm running it from the Terminal WITHIN VSCode.
Everything is working fine if I'm running the application from the Terminal outside of VSCode.
I'm running VSCode on macOS Big Sur Version 11.1 (M1 chipset).
I have python 3.8.2 and TensorFlow installed in a virtual environment.
Here's the steps to reproduce the Error. From the Terminal outside of VSCode I run
source env/bin/activate to activate the virtual environment
python to launch python. Output to the terminal (as expected): Python 3.8.2 (default, Nov 4 2020, 21:23:28) [...]
import tensorflow as tf
print(tf.__version__) This prints "2.4.0-rc0" to the terminal (as expected).
Now, if I repeat the exact same steps 1 and 2 in the built in VSCode terminal, I get the exact same output in 2. However, if I run command 3 and try to import tensorflow, the following error message shows up:
Traceback (most recent call last):
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found. Did find:
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/__init__.py", line 39, in <module>
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 83, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found. Did find:
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
It seems like the Terminal within VSCode is not using the same site-packages as the Terminal outside of VSCode, however, running print(sys.path) gives the same result.
The same problem happens if I'm trying to run my application within VSCode whereas it is working if I'm running it from the Terminal.
Any advice is greatly appreciated.
Try Python 3.7. Many people have complained tensorflow not working on 3.8, but working on 3.7. Also, try downloading it from the google tensorflow web page.
Piggy-backing off of Jay Mody in the comments above. I would first check that the shells you use in VS code is the same as the shell you are using in the non-VS code terminal.
Try the following shell commands:
echo $SHELL
If you get the same output, then I would suggest making sure the exact same python executables are being used. Try typing this in both of the shells:
which python
In VS Code, open an integrated Terminal, after activating the environment, run
pip show tensorflow
to check if the module exists in the current environment. If not, reinstall it.
I still don't know why this problem existed in the first place but I now have it resolved by installing python 3.8 via the ARM build of mini-conda.
Here are the steps.
Download mini-conda from here https://conda-forge.org/blog/posts/2020-10-29-macos-arm64/ and install it.
After installation, create a new Conda environment conda create --name python38 python=3.8. This will install the ARM version of python 3.8
Activate the new environment conda activate python38
Create a new virtual environment for your project. python -m venv myEnv
Download and unpack the ARM version of tensorflow https://github.com/apple/tensorflow_macos/releases
Run the included script to install tensorflow. /Volumes/SSD/Jan/Downloads/tensorflow_macos/install_venv.sh --prompt and point it to your newly created virtual environment of step 4.
With these steps, tensorflow could be correctly imported.
Some more reference which pointed me to this solution:
https://github.com/apple/tensorflow_macos/issues/8
https://github.com/apple/tensorflow_macos/issues/3

Struggling to get Django server to run in virtual environment

I'm new to using Django & Python and am having some difficulty trying to get my manage.py application to run in a virtual environment.
I am receiving the following errors at the top of my models.py file:
I initially thought that this was an issue with pylint not pointing at the right environment (when I switch my Python interpretter to the one outside of the virtual environment, the pylint errors disappear); however, whenever I try to run my server, I get the following error:
(.venv) tjmcerlean#LAPTOP-RKLCBQLO:/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce$ python manage.py runserverTraceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
File "/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce/.venv/lib/python3.8/site-packages/django/__init__.py", line 1, in <module>
from django.utils.version import get_version
ModuleNotFoundError: No module named 'django.utils'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 12, in main
raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
This error appears to suggest that the virtual environment is not activated. However, I activated it using the location of the virtual environment (.venv/bin/activate) and saw that the bash terminal now displays (.venv):
Additionally, when using the (pip freeze) command, I can see that Django is installed within the virtual environment too:
I'm lost as to why the server won't run and would really appreciate any advice.
Thank you!
Calling django.__file__ in activated env reveals the real path:
python -c "import django; print(django.__file__)"
it should looks like /usr/local/lib/python3.8/site-packages/django/__init__.py
then re-check PYTHONPATH environment variable.
Maybe you've installed 2 Django in venv and system-wide but in some of them there is no db. And also remove all of *.pyc files in your working folder.
The problems occur when you don't include a path in virtualenv.
Try adding the path of python on your vscode.
Or, try to install and uninstall the Django projects.

How to get numpy working properly in Anaconda Python 3.7.6

I am trying to use NumPy in Python. I have just installed Anaconda Python 3.7, and that all seemed to go smoothly. However, I cannot import numpy(using the line import numpy). When I do, I get the following error:
C:\Users\jsmith\anaconda3\lib\site-packages\numpy\__init__.py:140: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
Traceback (most recent call last):
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
from . import multiarray
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\multiarray.py", line 14, in <module>
from . import overrides
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import core
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\__init__.py", line 54, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
1. Check that you expected to use Python3.7 from "C:\Users\jsmith\anaconda3\python.exe",
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy version "1.18.1" you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was:
DLL load failed: The specified module could not be found.
I can see it in the Enviorments tab of Anaconda Navigator, and when I try to use it in Eclipse(Pydev) it shows up under forced builtins. I took a look at my PYTHONPATH, and both my enviorment in Eclipse and my base python directory (jsmith/anaconda3) are in it. I have tried importing other libraries I see under forced builtins,and those work fine, yet numpy seems to be the only one with issues. Calling pip install numpy tells me it is already installed with version 1.18.1. I looked at this stack overflow page, and ran the first command in the answer(conda create -n test numpy python=3.7 --no-default-packages) in anaconda prompt. This worked, and then I realized the test was specific to the question, and tried base instead, and got this error:
CondaValueError: The target prefix is the base prefix. Aborting.
However calling conda activate base did nothing.
As mentioned in the comments by #cel uninstalling and reinstalling numpy using pip uninstall numpy and pip install numpy made it work.
I better way is to
import os
import sys
os.path.dirname(sys.executable)
This will give you the path to your environment. Put the path into the settings for python
Open the Anaconda Prompt:
Then, you have to go to the Conda Environment that you want to use in PowerBI. Am having an environment 'temp', so I activate it first in the 'Anaconda Prompt':
(base) C:\Users\ashish>conda activate temp
Then I go to the directory having the "PowerBI" executable file in the installation folder:
(temp) C:\Users\ashish>cd "C:\Program Files\Microsoft Power BI Desktop\bin"
Then, I launch PowerBI from the Prompt:
(temp) C:\Program Files\Microsoft Power BI Desktop\bin>PBIDesktop.exe
This fixes the NumPy error you are getting. If you want any other package to use with PowerBI, install that package in the respective "Conda Environment" (in my case it is "temp").
Make sure the Python home directory (Anaconda3) has been added to the 'Power BI Desktop' global options in the Python scripting section too.

Anaconda environment cannot import module

I have installed PyCharm in order to work on Python projects. I have also installed Anaconda to create conda environments for my projects on my machine.
I have create a new project called 'hello-world-ml' and I have created a conda environment called 'hello-world-ml' for my python project.
Within, my python folder I have a script called 'hello-world.py'. Within this script I require the module 'sklearn'. I have activated my conda environment and installed sklearn using the command conda install scikit-learn. When I enter conda list. I can see that the module has been successfully installed within the conda environment.
I have also configure PyCharm to use the Python interpreter from the conda environment. So when I went to type from sklearn... it auto suggested what to import.
When I go to run my script, I get this error:
Traceback (most recent call last):
File "hello-world.py", line 1, in <module>
from sklearn import tree
File "C:\Users\Callu\Anaconda3\envs\hello-world-ml\lib\site-packages\sklearn\__init__.py", line 134, in <module>
from .base import clone
File "C:\Users\Callu\Anaconda3\envs\hello-world-ml\lib\site-packages\sklearn\base.py", line 11, in <module>
from scipy import sparse
File "C:\Users\Callu\AppData\Roaming\Python\Python36\site-packages\scipy\__init__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Users\Callu\AppData\Roaming\Python\Python36\site-packages\scipy\_lib\_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
I am unsure how to fix this error. Any help would be greatly appreciated.
In addition, when I deactivate the conda environment and try to run the script it works fine.

ImportError with VirtualEnv

I'm trying to use VirtualEnv for a Flask app that I'm creating since everyone has recommended me to do so. After creating my virtual environment, I installed the libraries that I needed using pip while the environment was activated. I'm running into ImportError problems with this script. The code works fine when I'm not in the virtual environment.
My script:
#!/usr/bin/python
import sc2reader
...
...
When I try to run it I get this:
(flaskapp)xxxx#xxxx-VirtualBox:~/flaskapp/bin$ ./test.py
Traceback (most recent call last):
File "./test.py", line 3, in <module>
import sc2reader
ImportError: No module named sc2reader
I've tried changing the shebang to reflect my VirtualEnv path for Python, but that didn't fix anything. The library is found in my site-packages folder in my virtual environment, so I'm not sure why I'm getting the ImportError.
I've never used VirtualEnv before so I'm assuming I configured it wrong so it's not seeing my site-packages.
Try using
#!/usr/bin/env python
as the shebang.
If that does not work, try seeing what is the output of which python.

Categories

Resources