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.
Related
I'm coding a bot.
In this bot, deep in the program directory structure, I have to make an import that needs the absolute path of a package far away in the directory structure. In a way that I can't make the imports.
I've managed to import it successfully by exporting the PYTHONPATH variable in my local ~/.bashrc file containing the absolute path to my package.
Then I can import things in my program like:
import absolute_path.module
The thing is, when someone else downloads this program files for use, or when I upload it to a server, how is this other party going to manage this absolute importing I made? (Provided the package to be imported is going along with the program files, in the same path where I make the importing).
They didn't set the PYTHONPATH variable, so, are they going to have troubles?
It depends. Is the other module something standard (ie installable via pip etc)? then you just add it to your project's requirements.txt and the users should be able to figure it out from there.
If it's something you've written, then you can use something like PyInstaller to package all the dependencies of your module (including imports and even the python interpreter) so users don't need to download anything extra.
Another option is to put the other module with your bot module and distribute them together, and use relative paths.
Make your bot into an installable package
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.
This issue has been driving me insane for the past few days.
So basically, I'm trying to port over a Pure Python project to a proper PyCharm project. This is to basically improve code quality and project structure.
I wish it was as simple as basically creating a virtualenv to house everything, but it isn't. This project will eventually be developed simultaneously by multiple developers with Git as source control, and the default libraries will be modified. I presume this means that the libraries should ideally be tracked by Git in the end. Virtualenv shouldn't help here as far as I know because it's not portable between systems (or at least that's still being tested).
This project will also be, in the future, deployed to a Centos server.
So the only plan I can think of to successfully pull off this would be to simply bring in all of the libraries (which was done using pip install -t Libraries <ExampleLibrary>) into a single folder, with a __init__.py inside, and use them from other python files as a package within the Pycharm project.
Is this possible / recommended? I tried various methods to reference these libraries, but they all don't work during runtime. Somehow when the files in the library import something else from their own package, an ImportError is raised saying that there's no such module.
Will accept any other suggestions too.
Using Pycharm Community Edition.
EDIT: After having a good night's rest I think the crux of the issue is really just project organization. Before I ported it over to Pycharm the project worked as expected, but this had all of the python files in the root directory, and the libraries in a subfolder of the root, with every project file having the same boilerplate code:
import os, sys
absFilePath = os.path.dirname(os.path.abspath(__file__));
sys.path.insert(1, absFilePath + "/lib")
I was hoping that by using Pycharm to help me flesh out the packages, I could avoid having repeated boilerplate code.
Note: Not full solution.
The addition of the template code below forces the file containing the code to be in the same directory as the libs folder.
For Pycharm, all I had to do was mark the libs folder as a source folder. Even with the addition of the template code to the file, the modified libraries still work as expected.
For the Python Shell, this template code is still needed:
import os
import sys
absFilePath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(1, absFilePath + "/lib")
How can I use the python-dateutil library in Google App Engine? I am using the webapp2 framework and need to know how to include the library and use it in my script.
EDIT2: Here's the recommended way to do this now: https://cloud.google.com/appengine/docs/python/tools/libraries27#vendoring
EDIT: Thanks to #TimHoffman, the correct (App Engine) way to do this is documented here. Ignore what I said below.
I've actually had to do this exact thing. First, I created a folder in my app project called 'lib' to hold any python libraries that aren't included with App Engine (for project organization). Then, I downloaded the dateutil python source and placed it in the new 'lib' folder. Finally, in your actual app code, before importing the desired libraries, you must add this line:
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
which just places the 'lib' folder in the python path so that python knows where the module actually is. Then simply:
import dateutil
Alternatively, you could just put the module code directly in your app folder, and python will automatically look in your program's folder for the module. Also, make sure it's included somewhere in your app.yaml so that it actually gets uploaded to google's servers.
The key, though, is that you must include the code for the module somewhere with your app.
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.