PyCharm errors on valid same-directory imports - python

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

Related

.exe made by Pyinstaller isn't recognizing a module. The module in question was defined in the main file

Not sure if it is relevant but I am using pycharm.
This is how my main file looks like
How my total directory looks like
When the CMD is turned on, the app crashes without and warning errors, when it is turned off it can't find the python application one file. I'm really not sure what I should try from here.
edit: the code was ran in the pycharm shell.
edit: It looks like the star declaration might be the cause, does anyone have any suggestions of what I can replace the star with?
edit: the pyApplication uses also uses the cooking module

Python script works in PyCharm but not in terminal

I'm currently trying to import one of my modules from a different folder.
Like this...
from Assets.resources.libs.pout import Printer, ForeColor, BackColor
This import method works completely fine in PyCharm, however, when i try to launch the file in cmd or IDLE, i get this error.
ModuleNotFoundError: No module named 'Assets'
This is my file structure from main.py to pout.py:
- Assets
- main.py
- resources
- libs
- pout.py
Any clue about how i could fix this ?
Any help is appreciated !
Edit: The original answer was based on the assumption that the script you're running is within the folder structure given, which a re-read tells me may not be true. The general solution is to do
sys.path.append('path_to_Assets')
but read below for more detail.
Original answer
The paths that modules can be loaded from will differ between the two methods of running the script.
If you add
import sys
print(sys.path)
to the top of your script before the imports you should be able to see the difference.
When you run it yourself the first entry will be the location of the script itself, followed by various system/environment paths. When you run it in PyCharm you will see the same first entry, followed by an entry for the top level of the project. This is how it finds the modules when run from PyCharm. This behaviour is controlled by the "Add content roots to PYTHONPATH" option in the run configuration.
Adding this path programmatically in a way that will work in all situations isn't trivial because, unlike PyCharm, your script doesn't have a concept of where the top level should be. BUT if you know you'll be running the script from the top level, i.e. your working directory will be the folder containing Assets and you're running something like python Assets/main.py then you can do
sys.path.append(os.path.abspath('.'))
and that will add the correct folder to the path.
Appending sys path didn't work for me on windows, hence here is the solution that worked for me:
Add an empty __init__.py file to each directory
i.e. in Assets, resources, libs.
Then try importing with only the base package names.
Worked for me!

Question regarding package management in PyCharm IDE

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.

Accessing tokens of a certain project in the embedded console of a Python IDE

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.

python unable to find module for import (on Mac)

I'll preface this question by saying that I've barely used python before, and never before on Mac OS, so I am fully ready to accept that I'm probably doing something rather silly!
I've been sent two python projects, one of which I need to run. When I open the project I'm interested in (in TextWrangler), and run, I get the following error:
context.py:16: ImportError: No module named fetch_command
Well.. fetch_command is a module in the other program, which is in the same directory (/Users/myname) as the program that I am trying to run. The (scant documentation for the applications suggests:
"I wouldn't try installing into your python installation dirs,
I'd install to some home directory or prefix and set up your
PYTHONPATH and PATH (or use virtualenv)"
and so I have tried (and succeeded - I've tested by calling echo $PATH and echo $PYTHONPATH) adding Users/myname to PATH and PYTHONPATH. This did nothing. I then tried adding /Users/myname/other_python_app/src to PATH and PYTHONPATH, but this also hasn't worked. Anyone know what I'm doing wrong..?
Thanks a lot in advance!
Ah, I understand the issue now. It's not that you can't find it in your Python project, but rather in your text editor. There should be a menu entry to set your PYTHONPATH from within TextWrangler (I tried looking for the documentation but the site seems to be down). Oftentimes, these editors don't respect the PYTHONPATH variable or do so only on restart.
This is a bit of a hack in your case, but try adding an empty file __init__.py so Python knows that this directory contains a module.

Categories