Jump to content

[Python3] file import to another file

WillLTT
Go to solution Solved by colonel_mortis,
3 hours ago, WillLTT said:

Because im not really importing a package thats in the default libs.

 

def packages() recives some variables converts them to imports and then imports that variable.

 

but i cant put this in main.py as it is:

 

1. Hella ugly code.

2. I rely on strings or variables from the same file somewhere else in the file

 

Heres how i convert(split) and import:


def packages(key, key2):
  import importlib
  import sys

  split_ = key.split("/")
  result_s = split_[0] + "." + split_[1]
  ext_rm = key2.split(".")
  globals()[ext_rm[0]] = importlib.import_module('{}.{}'.format(result_s, ext_rm[0]))

 

Correct me if I'm wrong, but as I understand it the issue is that this code is only importing into the global namespace of importer.py, so you can't access it as "os" from main.py?

 

Assuming that's the case, I would encourage you not to try that - instead, you should return the imported package from importer.py, then do

os = importer.import("os")

in main.py. You can even set it in the globals for main.py there if you really want os to be available globally within the module.

 

If you really want to make os available in the global scope of all modules when you run your importer (and once again I advise against this), you can use the builtins module, but to choose the name dynamically at runtime you need to evaluate code at runtime because builtins doesn't support dynamically choosing the name. For example,

import importlib
import builtins

def do_import(package):
  exec("builtins.{} = importlib.import_module(package)".format(package))

Here's an interactive demo, but (again) I do not recommend that you do this, there is almost certainly a better way to achieve what you're trying to do:

 

In the example:

You can see that im trying to import, but it will always import to import.py, im trying to make it import to main.py from running the function.

 

How can i do this?

# import.py

def importer(str_import):
	import str_import

# main.py, this is the file that will be running.

import importer
importer("os")

os.system("blah")

 

Link to comment
Share on other sites

Link to post
Share on other sites

In general, it's bad practice to use a reserved word (import, in this case) as a filename, function name, or variable name. It just creates confusion.

 

I see several problems with the file. First of all, your importer function won't work, since it will try to evaluate the "import str_import" line literally, and attempt to import a package called "str_import" which it won't find.

 

Why do you need this file anyway? Can't you just do "import os"

QUOTE/TAG ME WHEN REPLYING

Spend As Much Time Writing Your Question As You Want Me To Spend Responding To It.

If I'm wrong, please point it out. I'm always learning & I won't bite.

 

Desktop:

Delidded Core i7 4770K - GTX 1070 ROG Strix - 16GB DDR3 - Lots of RGB lights I never change

Laptop:

HP Spectre X360 - i7 8560U - MX150 - 2TB SSD - 16GB DDR4

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, RadiatingLight said:

In general, it's bad practice to use a reserved word (import, in this case) as a filename, function name, or variable name. It just creates confusion.

 

I see several problems with the file. First of all, your importer function won't work, since it will try to evaluate the "import str_import" line literally, and attempt to import a package called "str_import" which it won't find.

 

Why do you need this file anyway? Can't you just do "import os"

Because im not really importing a package thats in the default libs.

 

def packages() recives some variables converts them to imports and then imports that variable.

 

but i cant put this in main.py as it is:

 

1. Hella ugly code.

2. I rely on strings or variables from the same file somewhere else in the file

 

Heres how i convert(split) and import:

def packages(key, key2):
  import importlib
  import sys

  split_ = key.split("/")
  result_s = split_[0] + "." + split_[1]
  ext_rm = key2.split(".")
  globals()[ext_rm[0]] = importlib.import_module('{}.{}'.format(result_s, ext_rm[0]))

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, WillLTT said:

Because im not really importing a package thats in the default libs.

 

def packages() recives some variables converts them to imports and then imports that variable.

 

but i cant put this in main.py as it is:

 

1. Hella ugly code.

2. I rely on strings or variables from the same file somewhere else in the file

 

Heres how i convert(split) and import:


def packages(key, key2):
  import importlib
  import sys

  split_ = key.split("/")
  result_s = split_[0] + "." + split_[1]
  ext_rm = key2.split(".")
  globals()[ext_rm[0]] = importlib.import_module('{}.{}'.format(result_s, ext_rm[0]))

 

Sorry, I still don't really understand what you're trying to do.

You can import a python package that's not in the default libs by just pointing to the location of the package.

QUOTE/TAG ME WHEN REPLYING

Spend As Much Time Writing Your Question As You Want Me To Spend Responding To It.

If I'm wrong, please point it out. I'm always learning & I won't bite.

 

Desktop:

Delidded Core i7 4770K - GTX 1070 ROG Strix - 16GB DDR3 - Lots of RGB lights I never change

Laptop:

HP Spectre X360 - i7 8560U - MX150 - 2TB SSD - 16GB DDR4

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, WillLTT said:

Because im not really importing a package thats in the default libs.

 

def packages() recives some variables converts them to imports and then imports that variable.

 

but i cant put this in main.py as it is:

 

1. Hella ugly code.

2. I rely on strings or variables from the same file somewhere else in the file

 

Heres how i convert(split) and import:


def packages(key, key2):
  import importlib
  import sys

  split_ = key.split("/")
  result_s = split_[0] + "." + split_[1]
  ext_rm = key2.split(".")
  globals()[ext_rm[0]] = importlib.import_module('{}.{}'.format(result_s, ext_rm[0]))

 

Correct me if I'm wrong, but as I understand it the issue is that this code is only importing into the global namespace of importer.py, so you can't access it as "os" from main.py?

 

Assuming that's the case, I would encourage you not to try that - instead, you should return the imported package from importer.py, then do

os = importer.import("os")

in main.py. You can even set it in the globals for main.py there if you really want os to be available globally within the module.

 

If you really want to make os available in the global scope of all modules when you run your importer (and once again I advise against this), you can use the builtins module, but to choose the name dynamically at runtime you need to evaluate code at runtime because builtins doesn't support dynamically choosing the name. For example,

import importlib
import builtins

def do_import(package):
  exec("builtins.{} = importlib.import_module(package)".format(package))

Here's an interactive demo, but (again) I do not recommend that you do this, there is almost certainly a better way to achieve what you're trying to do:

 

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

I think I understand what you are trying to do, so let me first phrase what it is I think you are trying to do and you can correct me there:

 

You wrote a custom library of functions you want to import into your own main .py file.

This would be similar to how someone would module.exports in node with the addition of a require on the main file.


If this indeed what you are trying to do, I have good news for you.

This is incredibly simple in python.

 

Simply create the file in the same directory, (not 100% necessary but saying to do so for simplicity sake), name it a NONRESERVED name (ie. don't name it os, import, pandas etc) I'll use chicken.py as an example.

After this, simply use the same statement you would use for any other wheel (or python module depending on nomenclature):

 

import chicken

There are some caveats to this, and some dos and donts so you'll want to read up on the process more, but hopefully that gets you started.
If I was incorrect in my base assumption, feel free to correct me and I'll try and help further.

Link to comment
Share on other sites

Link to post
Share on other sites

On 5/4/2020 at 12:34 AM, colonel_mortis said:

Correct me if I'm wrong, but as I understand it the issue is that this code is only importing into the global namespace of importer.py, so you can't access it as "os" from main.py?

Ding ding ding.

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

×