Import python from different folder without using .py extension - python

Import python from different folder without using .py extension
Below is my python script (feed_prg) that calls the python script (distribute)
Please note that my script are at different location
feed_prg is at location /opt/callscript
#!/usr/bin/env python
import sys
sys.dont_write_bytecode = True
sys.path.append('/home/username')
import distribute
# Main method start execution
def main(argv):
something = 'some'
distribute.feed(something)
if __name__ == "__main__":
main(sys.argv[1:])
distribute is at location /home/username
#!/usr/bin/env python
import sys
def feed(something):
print something
def main():
something= "some"
feed(something)
if __name__ == "__main__":
main()
I am seeing the below error while executing ./feed_prg , and only when my distribute filename is just distribute and not distribute.py
Traceback (most recent call last):
File "./feed_prg", line XX, in <module>
import distribute
ImportError: No module named distribute
the distribute also has the execute privilege, as below
-rwxr-xr-x 1 username username 3028 Dec 16 21:05 distribute
How can I fix this. ? Any inputs will be helpful
Thanks !

It is not possible to do this using import directly. It's best to simply rename the file to .py
That being said, it's possible to load the module into a variable using importlib or imp depending on your Python version.
Given the following file at path ./distribute (relative to where python is run):
# distribute
print("Imported!")
a_var = 5
Python 2
# python2import
from imp import load_source
distribute = load_source("distribute", "./distribute")
print(distribute.a_var)
Usage:
$ python python2import
Imported!
5
Python 3
#python3import
from importlib.machinery import SourceFileLoader
distribute = SourceFileLoader("distribute", "./distribute").load_module()
print(distribute.a_var)
Usage:
$ python3 python3import
Imported!
5

Related

how to make a file act as a python module

I want to make a file that is consisted of 3 python programs.
but, when I want to access one of the there files from one of them, it cant find the folder.
I made a init python file in it so python can recognize it as a module
my folder struct:
dlgo/
__init__.py
goboard_slow.py
gotypes.py
my goboard_slow:
from dlgo.gotypes import player
error:
Traceback (most recent call last):
File "C:\Users\asus\Desktop\dlgo\goboard_slow.py", line 2, in <module>
from dlgo.gotypes import player
ImportError: No module named 'dlgo'
Access as below:
from dlgo.gotypes import players
See here more info on "Guido's decision" on imports in python 3 and complete example on how to import in python 3.
maybe try from (filename) import (functionname)
Tl;dr:
from gotypes.py import player
when you specify path to a file, interpreter starts looking for it inside same folder, unless you give it path from main dirrectory like '/' or 'C:\'
You can import a py file with the following statement:
# Other import
import os
import sys
if './dlgo' not in sys.path:
sys.path.insert(0, './dlgo')
from dlgo.gotypes import player
NOTE:
For IDE like PyCharm, you can specify the import path using the Project Structure setting tab (CTRL+ALT+S)
Helpful stack overflow questions [maybe off topic]:
What is the right way to create project structure in pycharm?
Manage import with PyCharm documentation:
https://www.jetbrains.com/help/pycharm/configuring-project-structure.html

Python - ModuleNotFound error using Anaconda

I am trying to run this program:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from io import open
from multiprocessing import Pool
import buildingspy.simulate.Simulator as si
# Function to set common parameters and to run the simulation
def simulateCase(s):
''' Set common parameters and run a simulation.
:param s: A simulator object.
'''
s.setStopTime(86400)
# Kill the process if it does not finish in 1 minute
s.setTimeOut(60)
s.showProgressBar(False)
s.printModelAndTime()
s.simulate()
def main():
''' Main method that configures and runs all simulations
'''
import shutil
# Build list of cases to run
li = []
# First model
model = 'Buildings.Controls.Continuous.Examples.PIDHysteresis'
s = si.Simulator(model, 'dymola', 'case1')
s.addParameters({'con.eOn': 0.1})
li.append(s)
# second model
s = si.Simulator(model, 'dymola', 'case2')
s.addParameters({'con.eOn': 1})
li.append(s)
# Run all cases in parallel
po = Pool()
po.map(simulateCase, li)
# Clean up
shutil.rmtree('case1')
shutil.rmtree('case2')
# Main function
if __name__ == '__main__':
main()
and I keep getting this error:
File "C:/Users/Toshiba/.spyder-py3/temp.py", line 11, in <module>
import buildingspy.simulate.Simulator as si
ModuleNotFoundError: No module named 'buildingspy'
I already installed the package using pip more than one time and nothing changes.
What am I missing?
This is the source of this code.
That error might be due to having multiple python installations on your computer:
https://docs.python.org/3/installing/#work-with-multiple-versions-of-python-installed-in-parallel
Please add the following lines somewhere to your script (or to a new script) and run it once from Spyder, and once from the console and compare the output:
import sys
print("python: {}".format(sys.version))
# also add the following if running from python 3
from shutil import which
print(which("python"))
Buildingspy has to be installed using pip, I would recommend to install it using the command:
python -m pip install -U https://github.com/lbl-srg/BuildingsPy/archive/master.zip
Anaconda adds an Anaconda prompt to the Start menu, use that to make sure the path to python.exe is correct.
Once BuildingsPy is installed correctly, you will run into the problem that on Windows multiprocessing will not work from Spyder (or, from IPython/Jupyter), please also read this issue:
https://github.com/lbl-srg/BuildingsPy/issues/179
You will have to run your script from the command line.

import or install module from a command line script

I have a script that looks like this:
import pip
import sys
def main(argv):
...[does stuff]...
if __name__ == "__main__":
main(sys.argv[1:])
I would like to implement this function i found on stack that imports a package (or install it if nescessary).
def import_or_install(package):
try:
__import__(package)
except ImportError:
pip.main(['install', package])
My vision is that if i run a script on a random computer, if the packaged needed to run said script are not installed, the script does it automatically; Otherwise, import the module.
When I try to run it, I get a NameError stating that the modules I call within main() are not defined.
Link to where I found the function: Check if module exists, if not install it
What I understand in Question, if something like this :
import pip
not_installed = []
try:
import test1
except ImportError,err:
k= str(err).split(' ')
not_installed.append(k[-1])
for i in not_installed:
pip.main(['install',i])
Here test1 is the file to be run, convert it to cli accordingly
P.S: Checked for 1 import error message

How to import a function from a module in the same folder? [duplicate]

This question already has answers here:
import function from a file in the same folder
(4 answers)
Closed last month.
I am trying to separate my script into several files with functions, so I moved some functions into separate files and want to import them into one main file. The structure is:
core/
main.py
posts_run.py
posts_run.py has two functions, get_all_posts and retrieve_posts, so I try import get_all_posts with:
from posts_run import get_all_posts
Python 3.5 gives the error:
ImportError: cannot import name 'get_all_posts'
Main.py contains following rows of code:
import vk
from configs import client_id, login, password
session = vk.AuthSession(scope='wall,friends,photos,status,groups,offline,messages', app_id=client_id, user_login=login,
user_password=password)
api = vk.API(session)
Then i need to import api to functions, so I have ability to get API calls to vk.
Full stack trace
Traceback (most recent call last):
File "E:/gited/vkscrap/core/main.py", line 26, in <module>
from posts_run import get_all_posts
File "E:\gited\vkscrap\core\posts_run.py", line 7, in <module>
from main import api, absolute_url, fullname
File "E:\gited\vkscrap\core\main.py", line 26, in <module>
from posts_run import get_all_posts
ImportError: cannot import name 'get_all_posts'
api - is a api = vk.API(session) in main.py.
absolute_url and fullname are also stored in main.py.
I am using PyCharm 2016.1 on Windows 7, Python 3.5 x64 in virtualenv.
How can I import this function?
You need to add __init__.py in your core folder. You getting this error because python does not recognise your folder as python package
After that do
from .posts_run import get_all_posts
# ^ here do relative import
# or
from core.posts_run import get_all_posts
# because your package named 'core' and importing looks in root folder
MyFile.py:
def myfunc():
return 12
start python interpreter:
>>> from MyFile import myFunc
>>> myFunc()
12
Alternatively:
>>> import MyFile
>>> MyFile.myFunc()
12
Does this not work on your machine?
Python doesn't find the module to import because it is executed from another directory.
Open a terminal and cd into the script's folder, then execute python from there.
Run this code in your script to print from where python is being executed from:
import os
print(os.getcwd())
EDIT:
This is a demonstration of what I mean
Put the code above in a test.py file located at C:\folder\test.py
open a terminal and type
python3 C:\folder\test.py
This will output the base directory of python executable
now type
cd C:\folder
python3 test.py
This will output C:\folder\. So if you have other modules in folder importing them should not be a problem
I usually write a bash/batch script to cd into the directory and start my programs. This allows to have zero-impact on host machines
A cheat solution can be found from this question (question is Why use sys.path.append(path) instead of sys.path.insert(1, path)? ). Essentially you do the following
import sys
sys.path.insert(1, directory_path_your_code_is_in)
import file_name_without_dot_py_at_end
This will get round that as you are running it in PyCharm 2016.1, it might be in a different current directory to what you are expecting...

From imports doesn't work on linux

I have probably path inaccuracies but can't track it. I have a following statement:
from serial import SerialException
# from Utilities.UnitsFormat import UnitsFormat
from Loggers.MainLogger import NetworkLogger as Logger
import Utilities.Serial.SerialHandle.SerialHandle as SH
It works well on Windows buy fails to find path in Debian Linux:
> root#debian-armhf:/# cd usr/CROW/ATE/Drivers/PSU_PR_V2/
root#debian-armhf:/usr/CROW/ATE/Drivers/PSU_PR_V2# python PSU_PR.py
Traceback (most recent call last):
File "PSU_PR.py", line 79, in <module>
from Loggers.MainLogger import NetworkLogger as Logger
ImportError: No module named Loggers.MainLogger
root#debian-armhf:/usr/CROW/ATE/Drivers/PSU_PR_V2#
How can I resolve this while I remain simple with path management and support both operating systems?
I have tried this with no help:
import os
if os.name == 'nt':
sys.path.append("C:\CROW\ATE")
else:
sys.path.append("usr/CROW/ATE")
Assuming that the file you are manipulating the path from is located some where under the CROW/ATE directory. You could use this code to get a system independent path to that directory like so:
import re
import os
def get_project_dir():
return re.findall(''.join(['.*', os.path.join('CROW', 'ATE')]), os.path.abspath(__file__))[0]
and then do
sys.path.append(get_project_dir())
*I wasn't able to test this on windows but this should be close.

Categories

Resources