Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Zuccers

import .py project to another project

On 6/11/2019 at 10:11 PM, Slottr said:

You'll need the py file it in the same project folder

False. Its most user friendly for beginners thats true but you can import files from anywhere. You can even import folders. Actually most modules are folders that have python files in them. I will show example of json module. 

 

Bit of explaning. For someone who plans to advance in python.
When you create __init__.py file in folder, you are telling python that folder is module.
For example here is json module. 
http://prntscr.com/o21qkw
And if we import json, and do dir(json) we will get content of that folder
http://prntscr.com/o21re8

Oher stuff that we got in dir are located in tools.py and __init__.py
for example here is dumps code: http://prntscr.com/o21so0

There is a lot of flexibility with designin and organising objects, functions, modules with python.

test it out, have fun. Cheers. 

Recommended Posts

Posted · Original PosterOP

Hello once again smart people, i have a question... How does one import a .py file into a different project i.e. we have filenr1.py, filenr2.py and main.py, so... We want filenr1.py and filenr2.py with their information(code) to be imported into main.py, can anyone help me how to do it? 
Thanks in advance?

Link to post
Share on other sites

ctrl + c


Hello! Feel free to PM me anything. Here's my stuff below!

PC (Main)

Spoiler

CPU: i5-8400 CPU Cooler: Cryorig M9 Plus   Motherboard: Gigabyte B360M DS3H | RAM: Crucial Ballistix Sport 2x8 DDR4-2400 SSD: Inland 480GB SSD | Video Card: RX 570 4GB Strix OC | Case: Fractal Design Meshify C White TGPSU: EVGA SuperNOVA G3 750 Monitor: Sceptre 24" 1080p 75hz Webcam: Logitech C920 Keyboard: Modded FC660M | Mouse: G305

 

NAS:

Spoiler

Synology DS418J w/ 4x WD Red Pro 6TB RAID 10

Seagate 5TB external SMR 2.5" HDD

MS Office 365 - 1TB Onedrive

 

Phone/Tablet:

Spoiler

iPhone XR 64GB iPad Mini 4 128GB

 

Laptops:

Spoiler

Dell XPS 15 9570 i7-8750H + 1050 Ti MacBook Air 13" mid-2012

 

Audio:

Spoiler

 Audio-Technica AD700X + Modmic 4 KZ ZS10 Pro | Audio-Technica LP120USB Audioengine A2+

 

Link to post
Share on other sites
import pythonfile.py as importedfile

 

This way you can reference the functions you've written in pythonfile.py easily 

 

"importedfile" can be whatever you want


|| ℍ𝕚 𝕙𝕠𝕨 𝕒𝕣𝕖 𝕪𝕒 || 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: GTX 1080 || Memory: 16GB @ 3200 || Cooler: Noctua D15 || PSU: 750W EVGA G2 || Case: Define C

Link to post
Share on other sites
Posted · Original PosterOP

Okay, but it would mark the "pythonfile.py" as an unsolved referance, do i need a directory or something?

Link to post
Share on other sites
8 hours ago, Zuccers said:

Okay, but it would mark the "pythonfile.py" as an unsolved referance, do i need a directory or something?

You'll need the py file it in the same project folder


|| ℍ𝕚 𝕙𝕠𝕨 𝕒𝕣𝕖 𝕪𝕒 || 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: GTX 1080 || Memory: 16GB @ 3200 || Cooler: Noctua D15 || PSU: 750W EVGA G2 || Case: Define C

Link to post
Share on other sites

Assuming all '.py' files are in the same directory, and 'file_to_import.py' is simply made up of a bunch of function I'd do:

from file_to_import import function_name_1, function_name_2
from file_to_import import *  # Imports all functions from 'file_to_import'

I wouldn't do the import with the ` * ` as it's a bit ambiguous.

 

If you have a directory structure like:

- main.py
- utils
  \ - thingy.py
  

If you want to import 'thingy.py' functions into the 'main.py' you can do:

from utils.thingy import function_name_1, function_name_2
Link to post
Share on other sites
Posted · Best Answer
On 6/11/2019 at 10:11 PM, Slottr said:

You'll need the py file it in the same project folder

False. Its most user friendly for beginners thats true but you can import files from anywhere. You can even import folders. Actually most modules are folders that have python files in them. I will show example of json module. 

 

Bit of explaning. For someone who plans to advance in python.
When you create __init__.py file in folder, you are telling python that folder is module.
For example here is json module. 
http://prntscr.com/o21qkw
And if we import json, and do dir(json) we will get content of that folder
http://prntscr.com/o21re8

Oher stuff that we got in dir are located in tools.py and __init__.py
for example here is dumps code: http://prntscr.com/o21so0

There is a lot of flexibility with designin and organising objects, functions, modules with python.

test it out, have fun. Cheers. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×