import a function from an other file in python - python

I know that this question has already been asked. But answers below these questions doesn't fix my problem. Here it is:
When I download some code from GitHub, it's always divided into separate files. I understand that it's important to have organized code, which is why I'd like to do the same.
However, whenever I try importing a function from a file, I always seem to get a ModuleNotFoundError error.
The file that I'm trying to import is in the same directory as the file importing the code. This also doesn't work with other code, for example, when I download code from GitHub that organizes code using separate files, it still returns the same error.
I've tried two different python installations (anaconda 3.7.3 and py 3.7.0), but still not luck. FYI I use pzyo to run my files.
Here's an example of how I import another file:
from fun import f
I have tried this as well:
import os
os.chdir("C:/Users/amau4/Desktop/test")
from fon import f
How would I go about fixing this? Thanks in advance!

Related

Why does VS studio give me an error when I try to import a function, class, etc

The problem is that, when I try to import a certain class from a .py archive that is in the same package it gives me an error. I have tried using from .something import function, from something import function and both ways end in error.
So your question is kinda vague but ive had a similar issue from what I understand in sublime text
to solve this make sure you have the correct python interpreter on visual studio

Jupyter Notebook: Import .ipynb file and access it's method in other .ipynb file giving error

I am fairly new to Jupyter Notebook. I have played around with it for a while. But this was my first time trying to import another notebook into my main class.
For reference, I am using Anaconda 4.3.1 and Python v2.7.
I am trying to replicate what I did in my python project to jupyter notebooks. It requires importing other .ipynb files (translated from the original .py files) into it to use relevant methods as desired.
For this, I followed the directions as given on Jupyter Nbviewer Steps Link which I found through my preliminary search on the following stack Question. It gave me some idea but didn't help me after one stage.
I will walk you through the steps I took and the sample program I tried.
Created a small .ipynb file abc.ipynb as follows
def prt_n(str):
print(str)
if __name__ == '__main__':
prt_n("in abc")
Created an .ipynb file to import Jupyter Notebook from the Jupyter link given above. Say, importer.ipynb.
Run importer.ipynb
import abc
str="Hello Me"
Test step abc.__name__ results in abc as output.
abc.prt_n(str) throws following error
*---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-2fb88a43c9e5> in <module>()
----> 1 abc.prt_n(str)
AttributeError: 'module' object has no attribute 'prt_n'*
I was hoping that the answer will be Hello Me.
Later, I want to actually create a myMain.ipynb file and in that I want to include 2-3 such notebooks and call their methods with required parameters.
One such example can be a file efg.ipynb as follows:
import abc
a="Hello Notebook"
abc.prt_n(a)
I also want to make sure if there is any other way to do this?
Note: I have already checked sys.executable and sys.path. Both have the same python path value.
Any sort of help is welcome!
Simple way to use ipynb files in jupyter note book are as follows:
1) Install import-ipynb
pip install import-ipynb
2) Import import_ipynb in jupyter notebook. Then import ipynb file as you import .py file
import import_ipynb
from test import print_name
print_name("your name")
**
Link to sample files on Drive
**
Ok. So, after some struggle and looking around on Internet, and finally found a solution that worked for my sample case.
Firstly, this is the stackoverflow question that was the most helpful to me. Mohideen and Tyhela's answer was the actual solution and not the one with most number of votes.
So, what I did was, I made a folder by the name module and placed all my .ipynb files there. Plus, I created an __init__.py file in that module by using touch __init__.py command such that the import can register it as a valid module. Those guys have given a detailed explanation for it which seems legit.
Then from my working directory I ran the following commands:
str = "Hello Me"
import test.abc as tabc
tabc.prt_n(str)
that gave me Hello Me in the output.
And for,
`import test.efg as tefg`
I got
importing Jupyter notebook from test/efg.ipynb
Hello Note
As my desired output.
I hope this will be of help to others who land up on similar problem.
If you have a better approach then I will appreciate if you can share that with me.
Thank you :)

Can't Install Vect2D

I'm following a pygame tutorial, and in it, you need to use multiple different modules. I was able to download all of them, except this one called Vec2D. When I try to run it in command prompt, it gives me this:
This is really weird because it gives no output - the module isn't set up and I don't get an error. When I try to test it later, it doesn't work. Does anyone know how to help me?
The instructor has written his own module, called vec2d_jdm.py containing a class called Vec2D.
You don't need to install it, just make sure that the file vec2d_jdm.py is in the same folder as the code you're running and from vec2d_jdm import Vec2D at the top of your code where you import everything else (such as pygame I imagine).

Splitting python code into different files (added picture of directory)

As my python code is getting longer, I would like to split it into a different files for better organization. In the same folder I created 2 files 'firstfile.py' and '2ndfile.py'
Below is the code of 'firstfile'
import pandas as pd
df=pd.DataFrame({'a':[2,4],'b':[2,1]})
Below is the code of '2ndfile'
import firstfile
print(firstfile.df)
Can I know why does the below error appear when i run '2ndfile'?
ImportError: No module named 'firstfile'
Hi, I tried the suggestions below including using dot something but it still does not work. Below is a screen shot of my directory. Is it related to some sys.path problem? I am currently using Spyder 2, python 3.5.
Try importing it with the from . import firstfile. Maybe you have Python3 which doesn't allow for implicit imports.
Switching from using Spyder2 python 3.5 to pyCharm solved the issue.

Can only import win32com.client with IDLE. What extra work do i need to set up pywin32?

I have a Tkinter program that i use for time management. I decided to have it scan my inbox in outlook to check emails for tags, and then based on the tag, add it to my list of tasks to do for the night.
The code i wrote works just fine, but i'm getting an error when I import win32com.client. I can import it in IDLE, but it is having problems importing when i try to run the code with a .bat file, or double clicking the .py file.
I have found several people with similar problems, but from what i can tell, it's having problems with win32api module, or pywin32
Traceback (most recent call last):
File "my_program_filename.py", line 1, in <module>
import win32com.client
File "c:/Python27/lib/site-packages/win32com/__init__.py", line 5, in <module>
import win32api, sys, os
ImportError: DLL load failed: The specified module could not be found
I'm really confused. When i get the sys.path it's the same with IDLE as it is running from the .py file with the exception of an added "c:/users/username/desktop/timer" for my .py file.
I'm really lost and haven't had to mess with the system path, but I'm not able to figure out what i need to do to fix this.
What do I need to do to get pywin32 working so I can use win32com.client?
Any help is greatly appreciated. Thanks!
IIRC, the problem was attempting to link to the debug build. I think i had to dupe the release build and rename it to be debug or something.
try building release and see if it "just works". if so, you have a direction to explore
this is an issue of not having the correct paths in the sys.path. If you make sure that all of the pywin32 folders are in the sys.path(If you check in IDLE it can show that the folders are included even when they aren't?!?!?).
You also have to make sure you run your code from inside your Python directory or it will fail to import win32api. I also found that if you do anything in a function that uses pywin32 and you accidentally misspell the function when you call it, the whole import fails without telling you your function is misspelled. You can also navigate to the /Python27/Lib/site-packages/win32com/client folder and run makepy.py to make sure the right Object library is installed.
When you run makepy.py, you select the COM object you want to use and it creates packages specific to what you want to use. I don't fully understand why this is, but once i did this and ran my file from the Python folder it worked! There is a more in depth explanation on how to get this working right here.
I found this link to be the solution rather than the win32com/client as indicated above: win32com import error python 3.4
in my case typing in cmd:
python C:\Python27\Scripts\pywin32_postinstall.py -install
I hope this helps

Categories

Resources