13

I have a file named handshake.py. Where there is a function send_data(argument). I want to import that function into another file named siptest.py. I am encountering two problems. I am using microsoft visual studio with windows 7, 64-bit. 1) I can't import function. I have tried using,

from handshake import*
handshkae.send_data(argument)

Which give me an error.

NameError: global name 'handshake' is not defined

Another option I have tried is using

import handshake
handshake.send_data(argument)

Which gives me an attribute error.

AttributeError: 'module' object has no attribute 'send_data'

If I use it the other way, such as

from handshake import send_data 

2) MS Visual studio says. No test discovered, please check the configuration settings but I still can run the test somehow. and it says that the test is failed because of Import Error.

ImportError: cannot import name send_data

Both of the said files are in same directory. Plus the function is defined in a class 'TCPhandshake' in handshake.py

Kashif Ahmad
  • 426
  • 3
  • 8
  • 22

10 Answers10

15

One possible reason: there exists reference cycle between module a.py and b.py:

In a.py: import b
In b.py: import a

The solution is to break the cycle. You need to make it clear which module should do what and reduce the dependence.

mari
  • 3
  • 2
Jing Chen
  • 151
  • 1
  • 3
3

Make sure both files are in the same directory and try:

from handshake import send_data

If this does not work, try to rename handshake.py file.

voyager_1
  • 61
  • 6
  • I have edited some information in last line. please look at that. May be it will give more clarity to my question and yes both files are in the same directory. I have tried by changing name. Both problems still persist as mentioned in my question. i.e. ImportError and MS Visual studio stops recognizing the tests – Kashif Ahmad Sep 13 '17 at 07:25
  • Did not notice your function was defined inside a class. Try import the class first, then instanciate the class: from file import TheClass theclass = TheClass() – voyager_1 Sep 13 '17 at 07:40
  • Thanks, @voyager_1, for your suggestion. I have had a similar problem when importing functions from a Py file. Changing the Py file name helps. – Tranle Jan 03 '23 at 02:59
1

Are both handshake.py and siptest.py in the same directory?

If not You can try this: 1) Add __init__.py empty file to the directory that contains handshake.py. 2) Then add the path to that directory to LD_LIBRARY_PATH and PYTHONPATH

Tejas
  • 284
  • 2
  • 8
1

make sure the function is in the path

import sys
sys.path.insert(0, '/directory/tothe/handshakefile/')

and then

import handshake
Areza
  • 5,623
  • 7
  • 48
  • 79
0

I had the same issue, That happened while I tried to run the program from another directory by using python /home/name/workspace/test.py

Fix I tired.

import sys
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

This need to be added at the beginning. This works for me.

Jishnunand P K
  • 145
  • 1
  • 8
0

I find a way to import files in the same directory by implementing keyword as and a object variable for example

import file as fileObj

But the downside is that when you want access to the imported files variable you have to first take fileObj.fileObjVariable.

Ginxxx
  • 1,602
  • 2
  • 25
  • 54
0

Try adding/updating the environment variable PYTHONPATH which should point to the folder that has handshake.py

proton
  • 658
  • 1
  • 7
  • 26
0

This is very simple, but for me it worked to restart the kernel.

user3262756
  • 649
  • 1
  • 9
  • 27
0

I got the same error using visual studio code. I literally just had to save the contents of the first and I was able to import the

CrazyNew
  • 11
  • 1
-1

Just make sure the files all are located in the root of the project, then this will work:

import handshake
handshake.send_data(argument)
Hamid Heydarian
  • 802
  • 9
  • 16