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.
Related
I'm using macOS 10.15.4 and PyCharm 2019.3.4
I currently have a folder inside the PyCharmProjects folder (that is automatically created when PyCharm first runs) where I keep projects for a class. I want to move this folder, which contains multiple other folders which each contain PyCharm projects, somewhere else on my computer (like the Desktop). The problem is if I move the folder (or even just a single PyCharm project) the next time I open the project in PyCharm, it says "Invalid python interpreter selected for the project." Now I can manually go into PyCharm preferences and point it to the new location I moved the folder so it can use the correct python interpreter. But this would be tedious to do for every single project I have. (And yes, every project I have uses its own interpreter and virtual environment.)
Is then a way I can move a folder containing multiple PyCharm projects without loosing the references to each of their respective python interpreters?
(Feel free to reword this question or the title.)
PyCharm uses configurations from your home directory. The docs say
macOS
Configuration
~/Library/Preferences/<PRODUCT><VERSION>
Caches
~/Library/Caches/<PRODUCT><VERSION>
Plugins
~/Library/Application Support/<PRODUCT><VERSION>
Logs
~/Library/Logs/<PRODUCT><VERSION>
where <PRODUCT> is PyCharm.
They are xml files. Among them also a list of configured interpreters. They are separate because they are available for all your projects to be chosen as the default interpreter. You'll have to edit the xml files.
For some reason PyCharm does not help with a moved virtualenv directory directly. (see comment from engineer: https://youtrack.jetbrains.com/issue/PY-32435#focus=Comments-27-3139072.0-0)
On the upside, everything else seems to work, except the path to the venv python. That one can be changed through preferences/project preferences/python interpreter. Just click on "add", "existing", and select the python compiler in your project folder.
Try not to select the ".virtualenvs" python compiler, as if you fumble like I did, it seems it's a bit of a pain to get rid of it and go back to the right one. If you do that, renaming the project folder once more gets you back to square one and you get to try again.
Note that I'm using MacOS. YMMV.
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
I switched to PyCharm Professional in hopes of using the code coverage tool, but it seems to have broken my test configuration. I have a project with a project folder, config folder and test folder like so:
\project
\my_python_app
\test
\config\config.yml
In Pycharm Community, I was able to set the test path to the \test folder and the working directory to \project, which allowed me to access the config file. But when I switched to pro, it broke and said
ImportError: Start directory is not importable: '/home/project/test'
I need the working directory set to '\project' in order to access the config file. Any ideas on why this would be happening or suggested workarounds?
Pycharms importing behaviour can be quite annoying, I had similar problems in the past. I could resolve then using the "Source Folder" setting in the project structure tabs. E.g. set the whole project folder (and maybe test) as "Source".
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.
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.