Where is the python workspace path located in PyCharm? - python

I created a Python project using PyCharm (2019.3.3) in folder F:\FolderA then I realized I meant to create it in F:\FolderB. So I copied my project from FolderA to FolderB. Now when I try to install a package, PyCharm says:
Cannot start process, the working directory 'F:\Projects\FolderA\MyProject\venv\Scripts' does not exist
I come from C# background, and everything project related is saved in .csproj file. Does Python have a similar file where it writes project/workspace information? So I can point to the correct path?

Inside /venv/scripts folder there are three "Activate" files. It holds the path to the old directory.
Replace the old paths with new paths
Restart PyCharm
Install package
Here's a screenshot of the path to the files:

Related

imports issues in vscode (Python)

I'm trying to write a python library with the following structre on vsc. When I run tests I got an error no module named pytrader. pytrader is a python package it has an init on it. In pycharm i used to set the src folder as the source directory and everything works. Any help thank you
enter image description here
The action of set the src folder as the source directory in the PyCharm is to add the src folder path to the PYTHONPATH.
In the VSCode, it has no way to add it directly, but it's possible to edit it when you debugging, you can add this in the launch.json file:
"env": {"PYTHONPATH":"${workspaceRoot}/src"},
And you can add it directly in the python file like this:
sys.path.append("D:\\TestPlace")

Pycharm mistakes folder for module

Recently I switched computers and re-downloaded my python and pycharm, I tried activating my saved projects on the new computer, but I ran into problem, pycharm doesn't recognize the parent folder of some of the files, and mistakes it for a module.
The folder is called: "Enrichment_extra_stuff", and when I try to import file in that folder, from another file in that folder, it seems like pycharm recognize it, because it shows me all of the files inside it, but when I run the code, I get the error ModuleNotFoundError: No module named 'Enrichment_extra_stuff'.
Also weirdly, when I try doing explict import and just write import fr to import the file fr, then pycharm shows an error but when I run it, it works like it should.
I tried digging a bit on pycharm, but got confused and didn't found anything, my python interpreter version is 3.8 and I program on windows if that helps.
A folder (or better a directory) is not seen as a module, unless you put __init__.py file in it. This could be empty, or it should be the module content. This is Python: a module is either a file, or a directory with __init__.py
The second part is only for Pycharm: PyCharm is created to handle large projects, and often your program is not in the base (root) directory of your project, but in one (or more) subdirectories (e.g. src). So you should explicitly tell PyCharm which are the root directories. So, go to the project structure panel (the panel with the files, usually on left side), go to your "root" directory, and set with right mouse click on your base source directory: on the pop-up menu select Mark directory as, and then select Source Root.

Why I need to specify working directories and path?

Whenever I do a project for computer science, I have to make sure all of my files are located in the same folder, or I'll have errors. If I want to use a file from somewhere else, I have to insert it into the path. I do these things but don't fully understand what is happening or why. Why is the path changed in the runtime environment?
When you run a python script you are executing it in the current working directory /home/user/python.py for example. That means this script since it lives in /home/user has access to everything in that path. However you should be able to access any other directory from here as long as the permissions are setup right. You would do that by using relative paths. so for example /home/user/python.py could access a file that is /home/example/file.txt by giving it the path ../example/file.txt from the python project.
Have you tried adding the path using sys.path.append? If you don't want to do that every time then you can set (Windows) %PYTHONPATH% to include your custom path. That's what I do for my include folder.

"Cannot start process, the working directory ... does not exist" error in PyCharm

When I run my Main.py script, PyCharm keeps telling me Cannot start process, the working directory /home/myname/PyCharmProjects/MyProjectName/mypackage does not exist.
This error occurred after creating a package (mypackage) for test purposes, moving files to it (including Main.py), and moving the files back to the root folder afterwards.
The package mypackage was empty after that, but PyCharm still thought that Main.py is located in that package. I could still run the program until I deleted the empty package, but path errors occurred. After deleting the package, I was unable to start it.
I can still run the other files that used to be in mypackage and are now in my root directory again.
I can still create and run new files in my root directory.
It happens because when you create a file it automatically assigns the working directory to it's configuration, which of course is the one where you created it.
You can change that by going into Run -> Edit Configurations. Click on the folder icon in Script path: and correct the path to the file. Click OK to save and you should be able to Run the file again.
After testing for a bit, I've found a solution (but not an answer to why this error occurs in PyCharm):
Delete the file and create it again. (Or rename or move it and create a new file with its old name, both should work.)
Set the working directory correctly
1. File-> Settings
2. Build, Execution, Deployment -> Console -> Python Console
3. Working directory: [The path to the directory where the file you're currently working on resides.]
I had the same problem, mine is probably related to the explaination gave by the others, it comes from the dir .idea, files *.xml contain the variable $DIR_PROJECT$.
Therefore, as the attribution of a new path didn't work, I just deleted my .idea, that is automatically loaded each time I open my project's directory.
It automatically regenerated the .idea, asked for the script path... And it worked perfectly
CAREFUL => You will automatically lose your project settings, you are deleting the "settings file"
I was getting this same error, and the path in "edit configurations" was correct.
However, this is what eventually got my code working again.
1) I commented out all of the code in my file ("ctrl" + "a" + "ctrl" + "/")
2) I commented something I knew would compile in the file. (my list of imports)
3) I ran the python file.
This time, it actually completely compiled and after that I was able to uncomment the rest of my code and everything worked again.
The issue kept popping up over and over in PyCharm. So I created a new project and loaded the needed script. Then I provided the directory to path and assigned the default Python version that I wanted to use... and it worked. Then I was able to finally use "execute line in console" once again.
I had this problem because I renamed my project, it was "xx" I renamed it to "yy", what I did was I went through the directory of .idea in the "yy", in any of those files (all XML files) if there were the name "xx", I replaced it with "yy"
I experienced this problem after moving my project to a different root directory. None of the above solutions worked for me.
I solved it by opening my entire project folder, instead of just the python file I was trying to run. And then running the file I wanted, while the entire project was loaded into PyCharm.
open the qtdesigner work dictionary setting and choose your project path then click OK, don't not use the mysterious work dictionary path by default
In my case Run -> Edit Configuration didn't help.
I've solved it changing the value of "WORKING_DIRECTORY" attribute in .idea -> workspace.xml
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/your/correct/path/here" />
I am a beginner so appreciated this 6 year and 6 month discussion. I couldn't add a comment. I setup PyCharm 2022.1.1 and a Python 3.10.4 virtual environment with pyside2 and pyside6 on Ubuntu 22.04. I tried all the suggestions above and could get none of them to work for me. Following these instructions: https://pythonpyqt.com/how-to-install-pyqt5-in-pycharm/ both pyside2-designer (Qt5Designer) and pyside6-designer from my virtual environment were added as external tools. pyside2-designer failed with the working directory error and pyside6-designer worked without any error. Looking closer at pyside2-designer selecting the working directory with the browse button, I still could not create file - exit code was 127 - but the Qt5 Designer was available for existing ui files. I used pyside2 because PyQt5-tools could not be pip installed in my python3.10.4 virtual env.
This worked for me on PyCharm 2021.3.2:

Python works in PyCharm but not from terminal

I recently figured out how to import modules for unittesting in python. As a solution to this, I use:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from Dev.test import someclass
This works fine while running in PyCharm and I get the expected output. However, when I run from terminal I run into an error:
ImportError: No module named Dev.test
I have the init files where they are supposed to be but I'm lost as to why this is working in PyCharm but not from the terminal. I have not changed my path or anything in PyCharm as this code is supposed to be able to run with minimal modifications on other machines. Any idea as to why this is happening and what I might be able to do to fix it?
My folder structure is as follows
-Current
-Dev
-__init__.py
-test
- __init__.py
-someclass.py
-Tests
-__init__.py
-someunittest.py
I have tried running someunittest from the main folder as well as with a complete path but it only works in PyCharm
sys.path.append(os.getcwd()[:os.getcwd().index('Dev')])
I added this to my imports and it seems to have solved the problem. However, this doesn't seem like it would be the right way to do it; it will do for now.
When running a script from within PyCharm, it runs it in an environment with PYTHONPATH set to the list of all the folders that are marked "Sources Root" (with a blue folder icon) in the project explorer.
Outside of PyCharm, PYTHONPATH is not normally set. The first entry in sys.path refers to the current working directory where the script was run from. As long as you run your script with your terminal's working directory as the folder containing Dev, it should be able to find the Dev.test module, regardless of the extra entry added to sys.path.
Once you get the working directory correct, you should be able to remove the sys.path hack.
What #codewarrior has said about the PyCharm setting its own PYTHONPATH is correct. But sys.path didn't have my current working directory. So to get around this problem, I updated my PYTHONPATH (or you can edit sys.path).
Setting PYTHONPATH
export PYTHONPATH=$PYTHONPATH:`pwd` (OR your project root directory)
Updating sys.path
import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')
You can use insert/append based on the order in which you want your project to be searched.
HTH.
Pycharm uses a virtual environment. When you try run your program in your terminal this enviroment isn't active.
You need to build or upload your enviroment Pycharm with your libraries.
cd to the directory project and write in terminal:
source venv/bin/activate
I too have had this issue - and the PYTHONPATH setting set by PyCharm did seem to be the issue.
My alternative (as I was nearly finished writing the code) was to generate a setup.py - and install the classes/structure in my local virtual Python environment.
I would recommend trying out $ pip install . in your source directory. This will install your own packages for your project.
To add to similar answers here, PyCharm is doing some extra config for you before running your script. If adding your sources root to PYTHONPATH doesn't work then examine your run configuration in PyCharm for the script in question, there will likely be some more behind the scenes magic at play.
I had similar problem. I think the problem is that Pycharm modifies PYTHONPATH so before running your script:
cd to the file where python file resides
run export PYTHONPATH=.
run the script
You can also create "main" python file where you set the python path and then call the other modules

Categories

Resources