Jump to content

import .py project to another project

Zuccers
Go to solution Solved by MladenM,
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. 

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 comment
Share on other sites

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: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

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

Link to comment
Share on other sites

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: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

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 comment
Share on other sites

Link to post
Share on other sites

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 comment
Share on other sites

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

×