Folks... New to TideSDK. Want to use the tool to develop cross platform scientific applications. I need to import external modules and package them into the app. Is this possible?
Yes, I've done this on multiple occasions. What has worked for me is to place the folder with the module into the "Resources" directory of your TideSDK project and use your usual "import" to load the module.
Related
Basically, I can only reference my other files as modules when they are in a very specific location:
C:\Users\Dave\Desktop\Programming\Python.
If I want to create a new folder for a large project with multiple modules, say
C:\Users\Dave\Desktop\Programming\Python\Project1,
I can no longer import any modules and keep getting a ModuleNotFoundError. I've looked into it and it seems I need to add that folder to the Python Path somehow, but I couldn't find any answers on how to do it. My computer runs on Windows 10 if that matters.
I think the immediate solution to your problem/the answer to your question would be to use sys.path.append() at the top of your script.
import sys
sys.path.append("<ABSOLUTE/PATH/TO/YOUR/CUSTOM/MODULES/FOLDER>")
import custom_module
This isn't an ideal solution for any kind of prod use, however. Based on what you wrote in your question, this may not be what you're looking for. More info might help to craft a more stable solution:
Are you using virtual environments when running python? Do you just run python script.py or is there a specific version of python you're using that's installed elsewhere than the common C:\Program Files\Python?
When you say that you work on a project with multiple modules, does that mean there are custom modules that you/someone wrote or is it just that that project uses non-standard library modules, i.e. you had to pip install them?
Can we get an example of the code you're running and the folder structure of your project/where the modules are that you need?
I am new to Pycharm and need some help. I am working on a project that makes use of a large library of modules (specifically, Schrodinger; which allows for a lot of cool chemistry programs). Schrodinger requires the use of Python 2.7, if that makes any difference.
There are too many modules to install to the project directory. When I move the project directory to the location of the modules, my script becomes stuck on 'initializing'. I have attempted to import it as a package to no avail.
I have also tried to use the sys.path command, however a lot of the modules make use of other modules as well. So I that has become a pain very quickly.
How can I use these modules within Pycharm? And if there is no easy way, do you have a recommendation for an IDE that does have this feature?
Thanks
Pycharm doesn't identifies user defined modules which are not imported to Pycharm.
I usually mask the module as a Sources Root see the picture for more details. if the modules are in same project.
Alternative way: In your case import the external modules using File -> Open modules with open -> open in current window -> add to currently opened project this looks like two different projects. Now you can mark Sources Root for the complete module (i.e. learning) which you have imported.
import stackoverflow
Now pycharm can identifies the user defined modules.
How do I ship some standard modules from Python together with my code?
I'm writing an add-on for Anki, for which I need Queue and threading modules from Python2.7 standard library.
When I try launching Anki, I get ImportError: No module named Queue. I assume that is because Anki does not ship with full Python interpreter and if I am missing any standard modules, I am to bundle them myself.
From Anki docs on addons:
Standard Modules
Anki ships with only the standard modules necessary to run the program
- a full copy of Python is not included. For that reason, if you need to use a standard module that is not included with Anki, you’ll need
to bundle it with your add-on.
So my question is: what steps do I take to bundle standard Python modules threading and Queue together with my add-on?
Note that add-ons in Anki are just Python scripts that have certain extra modules available.
From the Anki doc:
For a simple one-file add-on, you can upload the .py file. For multi-file add-ons, please create a subfolder that acts as a Python package, and create a small .py file that imports that package. Using the Japanese support add-on as an example, the structure looks like:
japanese/file1.py
japanese/file2.py
japanese/__init__.py # can be empty; marks the folder as a package
japanese/<binary support files>
jp.py
To upload a multi-file add-on, please zip up the folder and the loader .py file and upload the zip.
The <binary support files> can be the modules you want.
Checkout html_cleaner and image-occlusion-enhanced
on Github if you want to see how others do it.
For anybody else who is wondering how to import a .so file (I was using a library that tried to import parser and discovered that parser.__file__ was a .so file) the answer is it's the same as a .py file:
Create a directory (mkdir parser), copy the .so file into that directory (cp parser.cpython-37m-x86_64-linux-gnu.so parser/) and then add an __init__.py to the directory (touch parser/__init__.py).
This is almost certainly not cross platform, but it worked for my needs.
Im trying to add some libraries within my app to make it more portable.
The problem is that the app is built with not this in mind.
So to be more specific I want to have a folder in my package called libs and keep a copy of the libs used in my app there.
For example lets say I use MySQLdb library. In my app I am importing it like this
import mysqldb
I want to be able to use the same code but having mysqldb inside libs folder.
Is that possible? I tried some things with __init__.py but I failed.
You shouldn't be putting MySQLdb anywhere inside your project. It's a third-party library, it should either be installed as a system package or inside your project's virtualenv.
You can just play around with sys.path and should achieve what you want. Just do at the beginning of your program:
import sys
sys.path = [$'path_to_your_lib_folder_here'$] + sys.path
This is not a best practice however and you need to be carefull especially if your software will be used as a third-party by other libraries.
I'm still pretty new to Python.
I have a Django website with several apps and a /libs directory. I need to add a couple cron jobs that will use my Django models. I've already worked all that out, no big deal.
I have a problem with my imports, though.
I would like to include these scripts in the App they generally belong-to. But when I execute the script from the commandline, it's unable to load modules from the /libs directory.
To illustrate specifically the dir structure, imagine this pseudo-code:
import ./../libs/mysharedlib.py
In other words the scripts and sharedlib are:
~/project/myapp/myscript.py
~/project/libs/mysharedlib.py
I get that I could just add ~/project to my PYTHONPATH but then I have to worry about doing this during deployment and it just feels broken.
Should I move my scripts out of my apps and put them in the ~/project? Is there a better way?
There is a better way, custom management commands solve this.
https://docs.djangoproject.com/en/dev/howto/custom-management-commands/
These let you write stand alone utility scripts. You can run these as a cron or just as utilities. They use the exact same paths as any other module in your django app.
While these solve import problems does your libs directory have a __init__.py file? Can you import your lib directory in your views? Or is your import just not working in your cron scripts? Either way custom managment commands address this.