Im developing an installer for a GNU/Linux distribution in Python using Eclipse+PyDev. For some tasks on it there is needed that the program runs with root priviledges, but I run Eclipse as a common user.
I had searched a lot of stuff on the Internet about how to run an app as root without having to run Eclipse with priviledges, but no a single clue of how to accomplish this in a "nice way". So I tried with the "gksu2" python module, with has the gksu2.sudo() functions in the same way as gksu in bash.
I created a new module, imported gksu2 and executed the main.py module of the app, but I got a "ImportError: No module named ui.regular_ui.wizard". It runs ok without gksu2 in eclipse, but it doesn't if I use it. I thought it was an environment variables problem, but the sys.path is ok.
The same error happens if I run the app from a terminal, outside of Eclipse. What do you think?
It seems like your PYTHONPATH is different outside/inside Eclipse. Try just removing the Python interpreter and adding it again to gather new paths -- if that's not enough, do: import sys;print('\n'.join(sorted(sys.path))) outside/inside Eclipse to know what's different and fix your paths inside Eclipse.
Related
I have been using PyCharm since I began learning Python because of its amazing UI that helped me learn a great deal about the language. As I progress into more advanced projects, I am beginning to prefer using a text editor / command line combo so that I can build my own venv's and have better access to source control. My question is, how does PyCharm manage custom local packages that I created so that I can import them wherever I want in the directory? For instance a project that I have built exclusively in PyCharm that runs will raise numerous import errors when trying to run that same project in VS code, or even a command line shell (yes I did have the PyCharm created venv activated before running on both attempts). For further examples, here is the project structure I am confused about:
RootDirectory
package_1_folder
__init__.py
pckg_1_class.py
program_using_pckg_1_folder
class_using_pckg1class.py
venv
The above structure has no issues being imported and used in PyCharm, however VS code / Sublime when used with command prompt / gitbash will raise either an ImportError or a ModuleNotFound error. I have even gone as far as adding the desired packages to my laptops windows PATH, using sys.path.append (I know this is not good practice I was only trying to get it to work), and even modified the .pylintrc file with the project path with no success. Any help explaining why these errors are happening would be greatly appreciated :)
NOTE:
I have been able to use the packages in VS code as long as the program importing the module is located at the root directory level, but not in its own folder in the root directory. Again, this statement WILL work in PyCharm, I just want to know how PyCharm is able to achieve this.
After numerous attempts to locate how the system was keeping tack of module within the IDE, I found that my answer was not visible from the IDE. I found a .idea folder in my root directory that contains a few .xml documents that manage the directory including where to read modules from.
This is somethings that has been bothering me since I started using PyCharm to program in Python. I have a two .py files, in the same directory, main.py and external.py. Inside main.py, I have import external at the top. PyCharm marks this as an error, but it runs fine both in the new zsh MacOS terminal and PyCharm itself, and I can use all the things declared in external.py as expected.
I've played around with it a bit, and (to my very limited knowledge) it seems that PyCharm detects imports like Python2. Thats a guess, though, as I am unfamiliar with that version.
Why does PyCharm do this, or am I the one to blame? If it's not my fault, how can I fix it?
My file structure is as follows:
Project-|
|-external.py
|-main.py
I want to use things from external.py in main.py, and I can, but PyCharm gives it a red underline.
From given description, it correctly imported your external module.
Did you create a folder inside your project folder?
When using subfolders for your main.py/external.py files, Pycharm might not by default correctly detect your import statement.
Pycharm should give you an error message for said import statement.
Maybe the error is not connected to the import statement but to your pycharm setup e.g. correctly setting up your python interpreter.
If you provide more information regarding your folder structure or the error message, that might help.
Please try to mark directory containing your python files as Sources Root, see https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
This is my directory structure
src\
dirCode\
dirSubCode\
test.py
Django\
DjangoApp\
Home\
views.py
From within views.py I'm trying to import a class (let's call it Main) from test.py. The line in test.py is simply
from dirCode.dirSubCode.test import Main
and I'm getting a ModuleNotFoundError when I try to run the server. I printed out the os.sys.path and the second entry points to 'src\' while the first entry is simply ''. Since this is not the only app from where I'm going to need to call "external" code I'm trying to avoid hard-coding
sys.path.insert(0,'path\to\src\dirCode\dirSubCode\')
if that would even work. I've tried looking through documentation, looking at other StackOverflow questions (Import module from subfolder), etc. but I'm at a loss as to how to do this. I've tried going into the src\Django\DjangoApp\Home directory and just running a python console and simply trying to import the class but it's giving me the same error.
One odd development that I'm running into is that when I try to run it from the Anaconda Prompt (yes, I'm running Windows) it doesn't work but when I run the file in PyCharm it does work. If that helps provide some insight as to what might be going on I'd appreciate the help.
The proper way to do this is to introduce a shared package that you can install into a virtualenv for each webapp. Either from an actual release, or by using
python setup.py develop
Form within the shared package you essentially create a symlink in the venv that allows you to just import.
A more modern and simpler version is
pip install -e .
inside the library. See Python setup.py develop vs install
I believe you need to set the PYTHONPATH variable, which is used to set the default search path for modules.
https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
As per the python docs the syntax is like so...
https://docs.python.org/3.6/using/windows.html#excursus-setting-environment-variables
To temporarily set environment variables, open Command Prompt and use the set command:
C:\>set PATH=C:\Program Files\Python 3.6;%PATH%
C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
C:\>python
Pycharm- in the Run/Debug configuration- sets it when you have it checked off (which is the default).
I have a python application that is trying to load some Java libraries (specifically Axis2 web services). When I add the necessary jars in Eclipse via PyDev Project Source Folders, everything seems to work fine. However, I want to be able to do this at run time by adding to sys.path, but then my application doesn't seem to work.
In both cases I can load the jars just fine, but something must be different for there to be different results. My question is, is there a difference between adding jars via the sys.path at run time with sys.path.append() versus passing -D to the jython interpreter?
The problem turned out to be a difference in the way that Eclipse starts up the Jython interpreter as opposed to starting Jython manually from the command line. In the Eclipse Run Configuration pane, there is a way to see the command that is used to run your application. Mine looked like this:
/usr/lib/jvm/java-7-openjdk-i386/bin/java
-classpath /usr/local/lib/jython2.5.3/jython.jar:...
org.python.util.jython
-Dpython.path=...
myScript.py
(Note: I added line breaks for readability)
So it seems that Jython is launched from Java and that the Java classpath had to be fed the paths in addition to just the python path.
I'm currently using the NINJA-IDE for Python, but I expect a generic answer which is not specific to the NINJA-IDE (though don't give me an answer which is specific to Eclipse, Wing, NetBeans, PyCharm or any other-than-NINJA Python IDE. If the answer is specific be it specific to NINJA).
I would like to use the embedded interactive console which comes with my IDE so that I can quickly examine some objects, functions and classes. However, when trying to import a module of my project in the interactive console I receive an ImportError. The list sys.path includes the directory in which the module resides. I've tried changing the current working directory to the module's directory using os.chdir(), but I still get ImportError. I've also tried (inside NINJA-IDE) right clicking my project and selecting "Add this Project to the Python Console" as illustrated in the following screenshot. Still getting ImportError.
How do I access functions/methods/objects/classes of my project in the embedded console?
EDIT: Specific details:
The project is named movie_recommendations and all the files reside in C:\...\ninja projects\movie_recommendations. The module's file I was talking about is recommendations.py. The file resides in C:\...\ninja projects\movie_recommendations among all the other files.
The value of sys.path when typed in the embedded console is ['C:\\Program Files\\Ninja\\Ninja.exe', 'C:\\...\ninja projects\\movie_recommendations'].
The error message I get for import recommendations in the embedded console is No module named recommendations.
The current dir via os.chdir isnt what you need. The interpreter has already started at that point. If anything, you would import the sys module and add the path to the sys.path
But overall, you have to tell your IDE's project what the PYTHONPATH should be. I dont know that IDE but I assume it would be in the project settings.
Eclipse, for instance, has a section both in the general settings and project specific settings, for adding paths.