how to import proper python module on virtual environment - python

I have this problem, that cannot import torch and torchvision well.
Currently, I am using the virtual environment, juneon, using Anaconda.
I think I set the library path and python path properly like the first following image.
Running the python file does not matter, However, during debugging, they cannot find those modules via VSCODE. I posted the second following image I've been seeing.
What should I check in order to solve the problem?
FYI, the third image is my file tree
Please let me know what I am missing!
Thank you in advance.

Related

Problem with running python code on VS Code

[enter image description here][1]
Hello there,
I am facing the problem you can see in the image.
[1]: https://i.stack.imgur.com/qjV9Z.png
I have python installed in the D directory and added to path, everything looks like it should work.
I am creating a virtual environment for every project I am working on and I don't have any problems running commands like "pip install numpy" but when I try to run my code, even trying to import libraries it fails and the output in the picture is not very significant to me.
Has anyone faced a similar problem? can anyone help me?
Thanks in advance!
Judging from your screenshot, you have a file named numpy.py somewhere in your PYTHONPATH, causing Python to import that particular file instead of the NumPy library you intended.
See this link for more information on how Python import works: https://bic-berkeley.github.io/psych-214-fall-2016/sys_path.html

How to fix 'ImportError: dynamic module does not define module export function (PyInit_cv2)' error in Python?

I'm running a code on deep learning, which uses the opencv module, by running python main.py (contains import cv2 statement), but always get the error 'ImportError: dynamic module does not define module export function (PyInit_cv2)'.
I've tried to reinstall my anaconda and create new virtual environments, but all got the same result. This problem really confuses me a lot and I've googled for many related problems, none of them works. I think the problem is something related to the environment and has nothing to do with the code, because I got the same result by simply run import cv2 in python prompt. The more confusing thing is that, even after I remove the opencv module, I also get the same problem, but not a ModuleNotFoundError. Does anyone can give me some advice? Thanks a lot!
I think I found one possible reason of this error.
Recently I was configuring the caffe environment on one server, I downloaded the source code of opencv-2.4.13 and compiled manually, added /usr/local/opencv-2.4.13/build/lib to $PYTHONPATH, and caffe worked well. After that, when I entered one of my virtual environment using conda activate py35, which uses python3.5, tried import cv2 in the python prompt, got the error above.
I'm not sure but I think the cause of the error is opencv-2.4.13 compiles a python2 interface so it can't be imported by python3. Python imports packages by searching the directories listed in sys.path, where $PYTHONPATH is in the second place after the current working directory (This is a great article introduces the mechanism of python finding packages). So when we enter the py35 environment, python will first look for $PYTHONPATH and find the opencv installed on the root directory instead of finding the opencv in the virtual environment using conda install opencv-python.
So there are two solutions of this problem:
Use python2 instead.
Remove /usr/local/opencv-2.4.13/build/lib from $PYTHONPATH.
which all work for me.
Similar post, might help:
ImportError: dynamic module does not define init function (initfizzbuzz)
Could you provide info on how you installed the CV module?
I had the same problem, which was caused by the cv2.so file in /usr/local/lib/python2.7/site-packages/cv2.so. After I deleted the file and use command sudo pip3 install opencv-python, it worked for python3.

A script in Tensorflow (1.4.0) needs to import 'reader.py' (which is also located in tensorflow) but fails. Not sure what to do about this

Preface: I am using Python 3.6.5, Tensorflow 1.4.0, and Unity Ml-Agents v0.3
I am using tensorflow(1.4.0) along with the Unity Ml-Agents git repository. In tensorflow there is a file called saved_model_utils.py that needs to import reader.py from a different file directory within the tensorflow package. My issue is that when I run the training program it fails at the import line in saved_model_utils.py. I have tried multiple different ways of specifying a direct path to file for the import but none of them satisfied the issue. I assume there is something I don't know that is making this difficult because it doesn't make sense that tensorflow would put a faulty script in there package. Any help is appreciated!
Here is a picture of the saved_model_utils.py:
saved_model_utils.py with reader.py highlighted in the project window
Here is a picture of the terminal and error when I run the scripts:
Terminal Error
Looking at the file paths, it seems like there's inconsistency in the folder names. I tried
from tensorflow.contrib.saved_model.python.saved_model import reader
and it's working. If you look at the file path in your first image, it's "saved_model" rather than "saved_models".
Hope this can fix your problem.

ImportError: No module named data_utils

I'm extremely new to python and I'm recently trying to understand more about Machine Learning and Neural Nets
I know this is a trivial question but I seem to be having problem importing data_utils on jupyter notebook. Can anyone please help
Note: I am not using Keras, and I am following the tutorial in this video.
I was also following the video and went through the same issue, after searching for a while; Here is the link from Github tensorflow_chatbot_required_files. You can download from here and copy it to your working directory (python file directory).
Now you will be able to import both.
Based on the link that you provided for the video you are using, go to
this link and download both files into your working directory.
The files you need to download are data_utils and seq2seq_model.
But before doing this tutorial try the tensorflow tutorials found on the tensorflow website, to get you started with this library.
Also, if you are very new to python, I recommend this tutorial first.
Since you mentioned about Machine Learning and Neural Nets, I'll assume you're referring to Keras.
I'll also assume that you installed it via
pip install keras
In that case, you need to uninstall it first by running
pip uninstall keras
Then, clone it from Github, cd into its directory and run
sudo python setup.py install
More information here
I'm assuming you are using code from tensorflow's github, in that case you need to download https://github.com/tensorflow/models/blob/master/tutorials/rnn/translate/data_utils.py into your folder also.
what you are trying to do is importing a self-defined module, in order to do that do as follow:
in your case the module is called 'data_util' which contains functions that will be called later as data_util.'function name'.
let say that the data_util which is a python file (.py) is in this directory (C:/Users/xxx/modules), so all what you have to do is to run this line of code in order for python to find your modul when you call import data_util:
import sys
sys.path.append('C:/Users/xxx/modules')
import data_util
I too was using same files.
Just open seq2seq_model.py and in line 35 remove from TensorFlow and keep it just import dat_

No module named itk (Insighttolkit) in Python

I am novice in python and Simple_ITK both to process .mha 3D image files. I have two problems:
When ever I install ITK, I always get an error as shown below:
and somehow I install ITK It doesn't work at all having no connectivity with python. And I also tried python-insighttoolkit3 package then it shows and error of
" Can't down load Python 2.8:i386" in Ubuntu software Center. I have tried various methods to install Insight Tool Kit in Ubuntu whatever I found but most of the time I get the same error as shown in picture above.
So can anybody guide me how to install and build ITK so that no error like no module named itk found.
Now another problem is I have a .py file and it has a class in which I have to fed input externally otherwise it will consider as None. SO I tried to call the file as
import /path/to/file/xxx.py
but it gives an Syntax error: invalid syntax
so how to call class from a python file from python Console .
Now, I'm using Ubuntu 15.02. For any missing details please ask.
Thanks.
1- For what I see in the image, I see that you are trying to install ITK instead of SimpleITK. For SimpleITK it is very simple using a linux package installer, like pip or easy-install. in this link you have the instructions to install it correctly. Remember to execute the instructions as super user. Maybe it is the reason of your error ( I can't see the instruction you are writing in the console ).
2- I recommend you to read the Python tutorial for modules The fastest way is to be located first on the directory of your .py file. The sequence would be:
cd /path
import module.py

Categories

Resources