I am installing specific module from Github, but i am having problems using it's functions.
These are steps i took to install module:
Downloaded the zip file and unzipped it normally.
Launched setup.py with install option. (python setup.py install)
The module didn't have any documentation, so i checked setup.py and it's name was "Exchange".
I tried importing the module ( import Exchange ) and it worked.
Now since i couldn't find any documentation, i viewed exchange.py from github ( /Exchange/exchange.py ).
I tried using one of the functions, didn't work.
Then i realized that i was in folder file, so:
i imported exchange.py itself ( from Exchange import exchange).
Now from exchange.py i imported Exchange class (from Exchange.exchange import Exchange ).
I tried to call the class, (Exchange), but i needed to specify 7 arguments for __init__.
and again, i realized that i needed to launch the exchange.py itself, so i would have no problems, this is where i got stuck, i couldn't launch it.
How could i launch the module properly? Am i right that i need to start from exchange.py? If so how could i launch it? If not then what's the proper entry point?
Fixed the problem, Thanks to #metatoaster.
The entry of module, is main function from exchange.py, after calling it, module will be started.
So to start application, you need to import exchange.py from Exchange package, and from exchange.py, import and call main function.
from Exchange.exchange import main
main()
Related
In my company we decided to structure own python modules using this convention:
dsc.<package_name>
It works without any problem when two modules is used in other project that doesn't follow this convention. However, when in a develop environment I try to develop a new module "dsc.new_module" that references to other, for example, "dsc.other_module", the import raises a not module found exception. Is there any way to solve this?
If I package the module and install, everything is correct but not when I'm developing the module that is not able to find it. The only way I overcome this problem is doing that:
try:
from dsc.other_module import send_message
except ImportError:
def dummy(a, b):
pass
send_message = dummy
Beacause the function is not essential.
What you can do is install your packages in development mode. pip install -e . (from the parent folder) After this the imports should work as you envision them, so the same as other packages that use them.
Development mode is not required, but it adds the benefit of implementing changes made to your code immediately.
I am struggling a bit to get a package to work, so I would like to just get the simplest possible test case to work.
Here is my current attempt.
Inside of a folder called Python_experiment I have two files: a jupyter notebook with the code
from .pleasejustwork import eat_muffin
and a file called pleasejustwork.py with the code
def eat_muffin():
print('i ate a muffin')
When I run the line from the jupyter notebook I get the error "attempted relative import with no known parent package". What am I missing?
The syntax you’re using is for a python package. To achieve this, place a file, __init__.py in the directory. It can be empty. If you’d like to use that package from anywhere, write a setup.py script to build and install your package. There are a lot of good tutorials on how do that, like in the python docs. Then you could do
from python_experiment.pleasejustwork import eatmuffin or from another file in the package from .pleasejustwork import eatmuffin
If you’re just trying to learn about modules, you can simply take out that leading period
from pleasejustwork import eatmuffin
I have searched through the existing questions on stackoverflow and wasn't able to find what I am looking for. Firstly, I am new to Python, I come from Ruby so some things seem unclear to me in Python. I learn by doing so I am writing my own python REST API client for a payment gateway, which I plan on releasing to PyPi. The problem I have is importing modules from different folders.
Let's say I have the folder structure like this:
my_project/src/lib/directory1/module1.py
my_project/src/lib/directory2/module2.py
In my_project/src/lib/directory1/module1.py I want to import a function defined in my_project/src/lib/directory2/module2.py so
# my_project/src/lib/directory2/module2.py
from lib.directory2 import module1
This doesn't work, it says ImportError: No module named directory2. I read that in Python you need to add the module to the PATH but I have gone to PyPi and took the first library from the top (SeleniumBase) to look at how the code is organised in the GitHub project and I don't see they do any stuff like that. Could you please explain to me how this works and how I can organise my codebase to be able to import modules in one module from different folders to build my own library?
I read this article https://docs.python.org/3/reference/import.html and tried what they say in section 5.7. Package Relative Imports but it doesn't work
In theory this should work
from ..subpackage2.moduleZ import eggs
But it doesn't. I get SystemError: Parent module '' not loaded, cannot perform relative import.
Import will only work if the module you are trying to import is in the same directory as the script that is doing the import.
Otherwise you have to specify the path.
Like this :
import my_project/src/lib/directory1/module1.py
A noob python question.
I have a package called TorCtl, I saved it in /root/A and create a file (useIt.py) in /root/A which uses the package and it works absolutely fine.
now the problem starts when I try to use this package within another project.
I copied TorCtl package into /root/B/C and created a new file (useIt2.py) (this file is used from another file that is the main for this package.)
when I run main I get import error from TorCtl package. for example:
from elixir import *
ImportError: No module named elixir
I have no idea why it should work in one folder and not another.
Can anyone point out what I am doing wrong please?
This question already has answers here:
How to include third party Python libraries in Google App Engine?
(6 answers)
Closed 8 years ago.
I have got a custom module called subprocess32.py, I have placed this in:
/Python/2.7/site-packages/subprocess32
along with __init.py__
I have tried importing this package / module in a python shell using
from subprocess32 import subprocess32
this works fine I can use the functions etc.
I want to use this module within my goolgle app engine application, I have tried
from subprocess 32 import subprocess32
I get the following error:
No module named subprocess32
I have also tried putting the subprocess32 folder and its contents within the apps folder, and point the sys.path at it before input but no joy.
Any help would be appreciated many thanks.
There's no point installing it in your site-packages directory. That only exists on your local machine, obviously: it won't be included when you deploy and you'd therefore get errors when you tried to import it in production. To prevent you from doing just that, the development server runs inside a sandbox that does not import from that directory either - and it also prevents you from tweaking sys.path.
Instead, just put it inside your project directory and import it from there.
Edit Actually, now I come to think of it, I don't think this will help you. You don't say what your subprocess32 module does, but if it's in any way related to the standard subprocess module, you simply won't be able to use it on GAE. There is no system for you to shell out to, and no way to execute arbitrary commands. You probably need to explain exactly what you're trying to do with that module.
Assuming that your subprocess32 is valid python module. You should copy and paste this subprocess32 module to your project root directory.
And then try to import it.